blob: 1d9b0222390d2769ee7f69d0d64c625c5069315f [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 7.2 or 7.2.1, refer to the <a href="../../72/javame/quickstart.html">NetBeans IDE 7.2, or 7.2.1 Java ME MIDP Development Quick Start Guide</a>.</p>
<h3>Contents</h3>
<img src="../../../images_www/articles/73/netbeans-stamp.png" class="stamp"
alt="Content on this page applies to NetBeans IDE 7.3"
title="Content on this page applies to the NetBeans 7.3" >
<ul>
<li><a href="#reqs" title="Tutorial Requirements">Tutorial Requirements</a></li>
<li><a href="#install" title="Installing Oracle Java ME SDK">Setting Up the IDE with Oracle Java ME SDK (Windows only)</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="http://www.netbeans.org/downloads/index.html" target="_blank">NetBeans IDE with Java ME</a></td>
<td class="tbltd1">Version 7.3</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>
<tr>
<td class="tbltd1"><a href="http://www.oracle.com/technetwork/java/javame/javamobile/download/sdk/index.html">Oracle Java ME SDK</a></td>
<td class="tbltd1">Depends on the operating system </td>
</tr>
</tbody>
</table>
<p class="alert"><b>Important:</b> The distributions of NetBeans IDE 7.3 bundle neither Sun Java Wireless Toolkit nor Oracle Java ME SDK (both of them must be downloaded and installed separately).</p>
<p class="alert"><b>Important:</b> The required version of Oracle Java ME SDK varies depending on your operating system:</p>
<ul>
<li>The Windows distributions of NetBeans 7.3 require <a href="http://www.oracle.com/technetwork/java/javame/javamobile/download/sdk/index.html">Oracle Java ME SDK 3.2</a>.</li>
<li>The OS X version of the NetBeans IDE requires <a href="http://www.oracle.com/technetwork/java/javame/downloads/sdk30-mac-jsp-137878.html">Oracle Java ME SDK 3.0 for Mac OS</a>.</li>
<li>The Linux distribution of the NetBeans IDE requires <a href="http://www.oracle.com/technetwork/java/download-135801.html">Sun Java Wireless Toolkit 2.5.2</a>.</li>
</ul>
<h2><a name="install"></a>Setting Up the IDE with Oracle Java ME SDK (Windows only)</h2>
<p class="alert"><b>Important:</b> This section applies to Windows platforms only.</p>
<p>To set up the NetBeans IDE with the Oracle Java ME SDK for the
Windows platform, you need to: </p>
<ul>
<li>install Oracle Java ME SDK </li>
<li>activate the Java ME feature in the NetBeans IDE</li>
<li>install the Oracle Java ME Platform SDK in the NetBeans IDE </li>
</ul>
<p><b>To install Oracle Java ME SDK:</b></p>
<ol>
<li>Go to the <a href="http://www.oracle.com/technetwork/java/javame/javamobile/download/sdk/index.html">Java ME SDK download</a> page.</li>
<li>Click the Accept License Agreement option.</li>
<li>Download the <a href="http://download.oracle.com/otn-pub/java/java_me_sdk/3.2/oracle_java_me_sdk-3_2.exe">oracle_java_me_sdk-3_2.exe</a> installer file and save it to any location on your computer.</li>
<li>Double-click the installer file to run the installer, and follow the instructions.<br>
When the installation is complete, the Java (TM) ME Platform SDK 3.2 Device Manager icon ( <img src="../../../images_www/articles/73/javame/quickstart/icon.png" alt="Java (TM) ME Platform SDK 3.2 Device Manager icon" border=1>) appears in the Windows system tray.</li>
</ol>
<p><b>To activate the Java ME feature:</b> </p>
<ol>
<li>Choose Tools &gt; Plugins from the main IDE's toolbar.</li>
<li>In the Plugins dialog box, click the Installed tab and select Java ME in the list of available features.
<p class="align-center"><a href="../../../images_www/articles/73/javame/imp-ng/plugins.png" rel="lytebox" title="Plugins dialog box">
<img src="../../../images_www/articles/73/javame/imp-ng/plugins-small.png" alt="Plugins dialog box" border=1></a></p></li>
<li>Click Activate.</li>
<li>At the Welcome panel of the Installer dialog box, click Activate.
<p class="align-center"><a href="../../../images_www/articles/73/javame/imp-ng/installer.png" rel="lytebox" title="Installer dialog box">
<img src="../../../images_www/articles/73/javame/imp-ng/installer-small.png" alt="Installer dialog box" border=1></a></p></li>
<li>When the activation is successfully completed, click Finish.</li>
<li>Click Close to close the Plugins dialog box.</li>
</ol>
<p><b>To install the Oracle Java ME Platform:</b></p>
<ol>
<li>Choose Tools &gt; Java Platforms.<br>
The Java Platform Manager dialog box displays.
<p class="align-center">
<a href="../../../images_www/articles/73/javame/imp-ng/jpmanager.png" rel="lytebox"
title="Name and Location panel">
<img src="../../../images_www/articles/73/javame/imp-ng/jpmanager-small.png"
alt="Name and Location panel" border=1></a></p></li>
<li>Click Add Platform.</li>
<li>In the Select Platform Type of the Add Java Platform wizard, select Java ME CLDC Platform Emulator and click Next.<br>The IDE displays the Choose directory to search for platforms dialog box.</li>
<li>Select the directory you installed the Java ME SDK Platform to (for example, <tt>C:\Java_ME_Platform_SDK_3.2</tt>) and click Open.</li>
<li>In the Platform Folders panel of the Add Java Platform wizard, ensure the platform you selected in the previous step is chosen and click Next.</li>
<li>As soon as the IDE detects the platform, click Finish in the Detected Platforms panel of the the Add Java Platform wizard.
<p class="align-center">
<a href="../../../images_www/articles/73/javame/imp-ng/jpdetected.png" rel="lytebox"
title="J2ME Platform detected">
<img src="../../../images_www/articles/73/javame/imp-ng/jpdetected-small.png"
alt="J2ME Platform detected" border=1></a></p>
The Oracle Java ME Platform is now registered in the Netbeans IDE.
<p class="align-center">
<a href="../../../images_www/articles/73/javame/imp-ng/jpregistered.png" rel="lytebox"
title="J2ME Platform registered">
<img src="../../../images_www/articles/73/javame/imp-ng/jpregistered-small.png"
alt="J2ME Platform registered" border=1></a></p></li>
<li>In the Java Platform Manager dialog box, click Close.</li>
</ol>
<p>Now your NetBeans IDE is set up for mobile development.</p>
<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</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 Default Package and Main Executable Class 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 Projects 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. </li>
<li>Double-click the <code>HelloMIDlet.java</code> file to display the source code 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="../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>
<p class="align-center"><a href="../../../images_www/articles/73/javame/quickstart/emulator.png" rel="lytebox" title="'Make my day' message in Java ME SDK default emulator"> <img src="../../../images_www/articles/73/javame/quickstart/emulator-small.png" alt="'Make my day' message in Java ME SDK default 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="alert"><b>Important:</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>
<li>In the Plugins dialog box, click Close.</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 Default Package and Main Executable Class</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/73/javame/quickstart/newjavameproject.png" rel="lytebox"
title="Flow view of Hello Midlet in IDE"><img src="../../../images_www/articles/73/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/73/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/73/javame/quickstart/screen.png" rel="lytebox"
title="'Make my day' in the Device Screen preview in the VMD's Screen view"><img src="../../../images_www/articles/73/javame/quickstart/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</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>JavaMEPhone1</tt> available in the Java ME SDK 3.2 is shown below.</p>
<p class="align-center"><a href="../../../images_www/articles/73/javame/quickstart/emulator-screen.png" rel="lytebox" title="'Make my day' displayed in Java ME SDK default phone emulator"><img src="../../../images_www/articles/73/javame/quickstart/emulator-screen-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 target="_blank" href="../../trails/mobility.html">NetBeans IDE Java ME Learning Trail</a></li>
<li><a target="_blank" href="http://docs.oracle.com/javame/mobile/mobile.html">Java for Mobile Devices Documentation</a></li>
<li><a target="_blank" href="http://docs.oracle.com/javame/developer/developer.html">Java ME and Java Card Documentation</a></li>
</ul>
</body>
</html>