blob: ff29d4af3e9b5fc287798b4bdf54aec845984ab7 [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">
</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 "MyHello" that displays the text "Make My Day" 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.8, refer to the <a href="../../68/javame/quickstart.html">NetBeans IDE 6.8 Java ME MIDP Development Quick Start Guide</a>.</p>-->
<h3>Contents</h3>
<img src="../../../images_www/articles/69/netbeans-stamp-69-70-71.png" class="stamp"
alt="Content on this page applies to NetBeans IDE 6.9 or later"
title="Content on this page applies to the NetBeans IDE 6.9 or later" />
<ul>
<li><a href="#reqs" title="Tutorial Requirements">Tutorial Requirements</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="#source" title="Creating Applications in the Source Editor">Creating Applications in the Source Editor</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 6.9 or later </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 distribution of NetBeans 6.7 and later comes bundles with the Java ME SDK 3.0.</li>
<li>The Linux distribution of NetBeans IDE is bundled with the Sun Java Wireless Toolkit 2.5.2.</li>
<li>The MacOS X version of NetBeans 6.9 is bundled with the Java ME SDK 3.0 for CLDC.</li>
</ul>
<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>
<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>Check the <tt>Set as Main Project</tt> and <tt>Create Hello MIDlet</tt> check boxes (both are checked by default). Click Next. </li>
<li>Select the default as the emulator platform and use the remaining defaults. Click Finish. </li>
<!--li><a name="template"></a>Expand &quot;Configuration templates provided by
installed CLDC platforms&quot; and &quot;Sun Java Wireless Toolkit 2.52 for CLDC &quot;
folders. Check the boxes next to each of the configurations.
The IDE automatically creates a new project configuration for each template
listed.
-->
<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><img src="../../../images_www/articles/71/mobility/quickstart/newjavameproject.png" alt="Flow view of Hello Midlet in IDE" width="608" height="374"></p>
<p>Note: 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 show the Screen Designer window diplaying 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>.</li>
<p><img src="../../../images_www/articles/71/mobility/quickstart/stringedit.png" alt="The text 'Make my day' is typed in the text field of the Properties window." ></p>
<li>The Screen view displays a preview of the text you enter in the Text field.</li>
<p><img src="../../../images_www/articles/71/mobility/quickstart/motd-screen.png" alt="The text 'Make my day' is visible in Device Screen preview in the VMD's Screen view." ></p>
</ol>
<h3 class="tutorial">Compiling and Running the Project</h3>
<ol>
<li>Choose <tt>Run</tt> &gt; <tt>Run Main Project (F6)</tt> from the Run menu. 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.</li>
<p><strong>Tip</strong>: 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>DefaultFXPhone1</tt> available in the Java ME SDK 3.0 is shown below.</p>
<p><img src="../../../images_www/articles/71/mobility/quickstart/motd-emulator.png" alt="Make my day message displayed in Java ME SDK JavaFX phone emulator"></p>
<li>Click on the button below Exit to close the MIDlet. Then click on the button in the upper right corner of the device to close the emulator window. </li>
</ol>
</div>
<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. Next we create the <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 &quot;MID&quot; is in upper case letters). Specify the Project
Location that is different from the one you specified for the <a href="#vmd"><code>MyHello</code></a> project created earlier. We refer to this directory as <code>$PROJECTHOME</code> in this tutorial. </li>
<li>Check the Set as Main Project checkbox and remove the check from the Create Hello MIDlet checkbox. Click Next. </li>
<li>Select the default emulator platform and use the remaining defaults. Click Finish. </li>
<!--li>Expand &quot;Configuration templates provided by installed CLDC platforms&quot; and &quot;Sun Java Wireless Toolkit 2.52 for CLDC &quot; folders. Check the boxes next to each of the configurations. The IDE automatically creates a new project configuration for each template listed. </li-->
<li>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. 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
<pre class="examplecode">public class HelloMIDlet extends MIDlet</pre>
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, "Hello Test Code", 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("Exit", 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>
</ol>
<h3 class="tutorial">Editing the Java Source Code</h3>
<p>Now let's add some text for our MIDlet to display.</p>
<ol>
<li>In the <code>get_helloTextBox()</code> method, replace the example code, "Hello Test Code" with the text of your choice. For example, "Make My Day."
</ol>
<!--<p><b class="notes">Note:</b> For a complete guide to using the Java Source Editor in NetBeans see <a href="../../67/java/editor-tips.html">Java Editing in NetBeans IDE</a>.</p>-->
<h3 class="tutorial">Compiling and Running the Project</h3>
<ol>
<li>Choose <tt>Run</tt> &gt; <tt>Run Main Project (F6)</tt> from the Run menu. 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.</li>
<p><img src="../../../images_www/articles/71/mobility/quickstart/mmd-emulator.png" alt="Make my day message displayed in Java ME SDK 3.0 CLDC Clamshell emulator"></p>
</ol>
</div>
<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>