blob: c080a9c7b0feab4906b8120c2b4f11df9aff9a9f [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--
Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
-->
<html>
<head>`
<title>Java ME MIDP Development Quick Start Guide - NetBeans IDE Tutorial</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" >
<link rel="stylesheet" type="text/css" href="../../../netbeans.css">
<meta name="author" content="">
</head>
<body>
<h1>Java ME MIDP Development Quick Start Guide</h1>
<p>This tutorial guides you through the basic steps of using NetBeans IDE to create a Java&#8482; Platform, Micro Edition (Java&#8482; ME platform),
Mobile Information Device Profile (MIDP) application and is designed to get you started with mobile application development as quickly as possible. The tutorial takes you through some of the basic steps of working with the project system. We show you two ways to create a Java ME MIDP project named &quot;MyHello&quot; that displays the text &quot;Make My Day&quot; in a device emulator. This tutorial prepares you to use other IDE features for developing CLDC/MIDP applications.</p>
<p class="notes"><b>Note:</b> If you are using NetBeans IDE 6.9, 7.0, or 7.1, refer to the <a href="../../71/javame/quickstart.html">NetBeans IDE 6.9, 7.0, or 7.1 Java ME MIDP Development Quick Start Guide</a>.</p>
</ul>
<h3>Contents</h3>
<img src="../../../images_www/articles/72/netbeans-stamp.gif" class="stamp"
alt="Content on this page applies to NetBeans IDE 7.2"
title="Content on this page applies to the NetBeans 7.2" >
<ul>
<li><a href="#reqs" title="Tutorial Requirements">Tutorial Requirements</a></li>
<li><a href="#source" title="Creating Applications in the Source Editor">Creating an Application in the Source Editor</a></li>
<li><a href="#vmd" title="Creating Applications in the VMD">Creating an Application in the Visual Mobile Designer </a></li>
<li><a href="#nextsteps" title="Next Steps">Next Steps</a></li>
</ul>
<h2><a name="reqs"></a>Requirements</h2>
<p><b>To complete this tutorial, you need the following software and resources:</b></p>
<table>
<tbody>
<tr>
<th class="tblheader" scope="col">Software or Resource</th>
<th class="tblheader" scope="col">Version Required</th>
</tr>
<tr>
<td class="tbltd1"><a href="https://netbeans.org/downloads/index.html" target="_blank">NetBeans IDE with Java ME</a></td>
<td class="tbltd1">Version 7.2 </td>
</tr>
<tr>
<td class="tbltd1"><a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html" target="_blank">Java Development Kit</a> (JDK)</td>
<td class="tbltd1">Version 6 or 7 </td>
</tr>
</tbody>
</table>
<br>
<p><b class="notes">Note</b>: The emulator bundled with the IDE varies depending on which operating system installer is downloaded.</p>
<ul>
<li>The Windows distributions of NetBeans 7.1, 7.1.1, 7.1.2, and 7.2 come bundled with the Java ME SDK 3.0.5.</li>
<li>The Linux distribution of the NetBeans IDE is bundled with the Sun Java Wireless Toolkit 2.5.2.</li>
<li>The OS X version of the NetBeans IDE is bundled with the Java ME SDK 3.0 for CLDC.</li>
</ul>
<h2><a name="source"></a>Creating a MIDP Application Using the Source Editor</h2>
<div class="indent">
<p>Using the Source Code Editor, you manually create the code for your MIDlets. Creating code in the Source Code Editor gives you more flexibility when editing
the code, and enables you to insert preprocessor code blocks. You are going to create a <code>MyHello</code> application using the New Project and New File wizards,
and complete the code using the Source Editor. </p>
<h3 class="tutorial">Creating a New Java ME MIDP Project </h3>
<ol>
<li>Choose <tt>File</tt> &gt; <tt>New Project (Ctrl-Shift-N)</tt>. Under Categories, select <tt>Java ME</tt>. Under Projects, select <tt>Mobile Application</tt> and click Next.</li>
<li>Enter <code>MyHelloMIDlet</code> in the Project Name field (note that <code>MID</code> is in upper case letters). Specify the Project
Location. We refer to this directory as <code>$PROJECTHOME</code> in this tutorial. </li>
<li>Deselect the Create Hello MIDlet checkbox and click Next. </li>
<li>Select the default emulator platform and use the remaining defaults. Click Finish. <br>
The IDE creates the <code>$PROJECTHOME/MyHelloMIDlet</code> project folder. The project folder contains all of your sources and project metadata, such as the project Ant script. </li>
<li>Right-click the <code>MyHelloMIDlet</code> node in the Explorer window and choose <tt>New</tt> &gt; <tt>MIDlet</tt>.</li>
<li>Enter <code>HelloMIDlet</code> as the MIDlet name (note that &quot;MID&quot; is not in upper case letters by default). Click Finish.<br> The <code>HelloMIDlet.java</code>
file is created and the source code is displayed in the IDE's Editor window. </li>
<li>Click in the Source Editor and change <code>public class HelloMIDlet extends MIDlet</code> to
<pre class="examplecode">public class HelloMIDlet
extends MIDlet implements javax.microedition.lcdui.CommandListener
{</pre>
</li>
<li>Add the following text before the <code>startApp()</code> method:
<pre class="examplecode">
private void initialize() {
javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_helloTextBox());
}
public void commandAction(javax.microedition.lcdui.Command command, javax.microedition.lcdui.Displayable displayable) {
if (displayable == helloTextBox) {
if (command == exitCommand) {
javax.microedition.lcdui.Display.getDisplay(this).setCurrent(null);
destroyApp(true);
notifyDestroyed();
}
}
}
private javax.microedition.lcdui.TextBox get_helloTextBox() {
if (helloTextBox == null) {
helloTextBox = new javax.microedition.lcdui.TextBox(null, &quot;Hello Test Code&quot;, 120, 0x0);
helloTextBox.addCommand(get_exitCommand());
helloTextBox.setCommandListener(this);
}
return helloTextBox;
}
private javax.microedition.lcdui.Command get_exitCommand() {
if (exitCommand == null) {
exitCommand = new javax.microedition.lcdui.Command(&quot;Exit&quot;, javax.microedition.lcdui.Command.EXIT,
1);
}
return exitCommand;
}
javax.microedition.lcdui.TextBox helloTextBox;
javax.microedition.lcdui.Command exitCommand;
</pre>
</li>
<li>Add a line <code>initialize();</code> to the <code>startApp()</code> method, so it looks like the following:
<pre class="examplecode"> public void startApp() {
initialize();
}
</pre></li>
</ol>
<h3 class="tutorial">Editing the Java Source Code</h3>
<p>Now let's add some text for our MIDlet to display.</p>
<p>In the <code>get_helloTextBox()</code> method, replace the example <code>Hello Test Code</code> code with the text of your choice (for example, <code>Make My Day.</code>).</p>
<p class="notes"><b>Note:</b> For a complete guide to using code assistance features, customization options, and navigation capabilities of the NetBeans IDE's Java Editor see <a href="../../../kb/73/java/editor-codereference.html">Code Assistance in the NetBeans IDE Java Editor</a>.</p>
<h3 class="tutorial">Compiling and Running the Project</h3>
<p>Choose <tt>Run</tt> &gt; <tt>Run Project</tt> from the main IDE's toolbar. Follow the progress of the project compilation in the Output window. Note that the <code>HelloMIDlet.java
</code>file is built before it is executed. A device emulator opens to display the results of the executed MIDlet and displays the text you entered
in the source code.
<p class="align-center"><a href="../../../images_www/articles/72/javame/quickstart/mmd-emulator.png" rel="lytebox" title="'Make my day' message in Java ME SDK 3.0.5 default CLDC emulator"> <img src="../../../images_www/articles/72/javame/quickstart/mmd-emulator-small.png" alt="'Make my day' message in Java ME SDK 3.0.5 default CLDC emulator" border=1></a></p>
<p>To close the MIDlet and the emulator window, choose Application &gt; Exit in the emulator window.</p>
</div>
<h2><a name="vmd"></a>Creating a MIDP Application Using the Visual Mobile Designer</h2>
<p>The NetBeans IDE provides a wizard that enables you to quickly create a MIDP project. When creating the project, you can choose to develop
your application in the Visual Mobile Designer (VMD) or in the Source Code Editor. Using the VMD gives you the ability to visually plan out
the flow of the application and design the screens the application uses. The designer automatically creates the code for the application as
changes are saved on the design canvas. </p>
<p class="notes"><b>Note:</b> To run the Visual Mobile Designer in the IDE you need to install the Visual Mobile Designer plugin from the Update Center as follows:</p>
<ol>
<li>Choose Tools &gt; Plugins from the main menu.</li>
<li>On the Available Plugins tab of the Plugins dialog box, select Visual Mobile Designer and click Install. </li>
<li>At the Welcome page of the Installer dialog box, click Next. </li>
<li>At the License Agreement page, read the license agreements associated with the plugin. If you agree to the terms of all of the license agreements, click the appropriate option and then click Install.</li>
<li>(Optional) Select the Run in the Background option to run the installation in the background.</li>
<li>When the installation process is complete, click Finish. </li>
</ol>
<h3 class="tutorial">Creating a MIDP/CLDC Application </h3>
<ol>
<li>Choose <tt>File</tt> &gt; <tt>New Project (Ctrl+Shift+N)</tt>. Under Categories, select <tt>Java ME</tt>. Under Projects, select <tt>Mobile Application</tt> and click Next.</li>
<li>Enter <code>MyHello</code> in the Project Name field. Use the default Project Location, or change it to the directory you prefer on your system. In this tutorial we refer to this directory as <code>$PROJECTHOME</code>. </li>
<li>Select the <tt>Create Hello MIDlet</tt> check box. Click Next. </li>
<li>Select the default as the emulator platform and use the remaining defaults. Click Finish. </li>
<li>The IDE creates the <code>$PROJECTHOME/MyHello</code> project folder. The project folder contains all of your sources and project metadata,
such as the project Ant script. The application itself is displayed in the Flow Design window of the Visual Mobile Designer.
<p class="align-center"><a href="../../../images_www/articles/72/javame/quickstart/newjavameproject.png" rel="lytebox"
title="Flow view of Hello Midlet in IDE"><img src="../../../images_www/articles/72/javame/quickstart/newjavameproject-small.png" alt="Flow view of Hello Midlet in IDE" border=1></a></p>
<p class="notes"><b>Note:</b> For a complete description of the available palette components, please refer to the <a href="http://wiki.netbeans.org/VisualMobileDesignerPalatteReference">Visual Mobile Designer Palette Reference</a>.</p>
</li>
</ol>
<h3 class="tutorial">Editing the Java Source Code</h3>
<p>Now let's edit the text displayed by the MIDlet.</p>
<ol>
<li>Under <tt>HelloMIDlet.java</tt> click Screen to switch to the Screen View displaying the Device Screen, which is the only screen available in the application. </li>
<li>Click the <tt>Hello World!</tt> message in the Device Screen. In the Properties window click the <tt>Hello World!</tt> text to change the welcome message text and press Enter. In this example we typed <tt>Make my day</tt>.
<p class="align-center"><img src="../../../images_www/articles/72/javame/quickstart/stringedit.png" alt="The text 'Make my day' is typed in the text field of the Properties window." ></p></li>
<li>The Screen View displays a preview of the text you enter in the Text field.
<p class="align-center"><a href="../../../images_www/articles/72/javame/quickstart/motd-screen.png" rel="lytebox"
title="'Make my day' in the Device Screen preview in the VMD's Screen view"><img src="../../../images_www/articles/72/javame/quickstart/motd-screen-small.png" alt="The text 'Make my day' is visible in Device Screen preview in the VMD's Screen view." border=1></a></p></li>
</ol>
<h3 class="tutorial">Compiling and Running the Project</h3>
<ol>
<li>Choose <tt>Run</tt> &gt; <tt>Run Project (F6)</tt> from the main IDE's toolbar. Follow the progress of the project compilation in the Output window. Note that the <code>HelloMIDlet.java</code> file is built before it is executed. A device emulator opens to display the results of the executed MIDlet. The device emulator launches the MIDlet and displays the text you entered in the source code.
<p class="tips">You can specify the emulator to run the MIDlet on by right-clicking the project node and selecting <tt>Run With..</tt> from the contextual menu. The <tt>DefaultCldcPhone1</tt> available in the Java ME SDK 3.0.5 is shown below.</p>
<p class="align-center"><a href="../../../images_www/articles/72/javame/quickstart/motd-emulator.png" rel="lytebox" title="'Make my day' displayed in Java ME SDK default phone emulator"><img src="../../../images_www/articles/72/javame/quickstart/motd-emulator-small.png" alt="'Make my day' displayed in Java ME SDK default phone emulator" border=1></a></p></li>
<li>In the emulator window, choose Application &gt; Exit to close the MIDlet and the emulator window.</li>
</ol>
<div class="feedback-box"><a href="/about/contact_form.html?to=6&subject=NetBeans%20MIDP%20Development%20Quick%20Start%20Guide">Send Feedback on This Tutorial</a></div>
<br style="clear:both;" >
<h2><a name="nextsteps"></a>Next Steps </h2>
<p>For more tutorials showing how to developing MIDP and CLDC applications visit the Mobile Application Learning trail.</p>
<ul>
<li><a href="../../trails/mobility.html">NetBeans IDE Java ME Learning Trail</a></li>
</ul>
</body>
</html>