FeaturesPluginsDocs & SupportCommunityPartners

J2ME MIDP Development for NetBeans IDE 4.0
Quick Start Guide

This document takes you through the basics of using NetBeans IDE 4.0 to create a Java™ 2 Platform, Micro Edition (J2ME™ platform), Mobile Information Device Profile (MIDP) application. This document is designed to get you going as quickly as possible. More information about using the J2ME support in the NetBeans development environment is provided in the following documents:

The following short tutorial takes you through some of the basic steps of working with the new project system. We will create a J2ME MIDP project named "MyHello" that displays the text "Hello World" in a device emulator.


Requirements

You must have NetBeans IDE 4.0 and the NetBeans Mobility Pack 4.0 installed before you can start J2ME MIDP development. See the J2ME MIDP Development Download page for instructions on downloading and installing the complete environment.


Creating a MIDP Application


Creating a new J2ME MIDP Project

  1. Choose File > New Project (Ctrl-Shift-N). Under Categories, select Mobile. Under Projects, select Mobile Application and click Next.
  2. Under Project Name, enter MyHello. Change the Project Home to any directory on your system. From now on, we will refer to ths directory as $PROJECTHOME.
  3. Check the Create HelloMIDlet check box. Click Next.
  4. Leave the J2ME Wireless Toolkit as the selected Target Platform.
  5. Click Finish. The IDE creates the $PROJECTHOME./MyHello project folder. The project folder contains all of your sources and project metadata, such as the project Ant script. The MyHello project opens in the Projects window.

Editing the Java Source Code

Now let's add some text for our MIDlet to display.

  1. Expand the MyHello project node and double-click on the HelloMIDlet.java source code node. The source code displays in the Source Editor.
  2. In the startApp() method, replace the "test string" code with the text of your choice. For example, "Hello World."

Compiling and Running a Project

  1. Choose Run > Run Main Project (F6) from the Run menu. Double-click the Output window to maximize it so you can see all the output. Note that the HelloMIDlet.java file is built and pre-verified before it is executed. A device emulator opens to display the results of the executed MIDlet. The default device emulator is DefaultColorPhone.
  2. In the device emulator window, click on the button below the Launch command. The device emulator launches the MIDlet and displays the text you entered in the source code.
  3. 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.

Changing the Emulator Platform


Changing the Default Emulator Device

You can create different project configurations to test your MIDlet on different emulator platforms.

  1. Right-click the MyHello Project node and choose Properties. In the Properties dialog, choose the Platform node. You can change the device for the default configuration.
  2. Click the Device dropdown menu and choose QwertyDevice. Click OK.
  3. Run the application again, and the applicaton runs in the QwertyDevice emulator.

Adding a new Emulator Platform

  1. Choose Tools > Java Platform Manager from the main toolbar.
  2. In the Java Platform Editor dialog, click the Add Platform button.
  3. In the Choose Platform Folder page of the Add Platform wizard, Use the Browse button to navigate to the parent directory of the emulator platform you want to install. For example, c:\WTK22. Click Next.
  4. In the Configure the Platform Page, the IDE detects and configures the platform and displays information about the platform. Click finish to complete the configuration.
  5. Choose File > "MyHello" Properties.
  6. Choose the Platform node, then choose the name of the new emulator (for example, J2ME Wireless Toolkit 2.2) from the Emulator Platform dropdown menu. Click OK.

Advanced: Using Configurations


Adding a Configuration

You can use configurations to create more than one set of distribution JAR and Java Application Descriptor (JAD) files for your project.

  1. Right-click the MyHello Project node and choose Properties. In the Properties dialog, Choose Add Configuration from the Configuration combo box. This opens the Add Configuration dialog.
  2. Enter a name for your configuration in the Configuration Name field. The name should identify the target platform on which you will deploy the JAR/JAD created for this configuration. For this example, enter BlackWhiteDisplay and click OK. You have just created a new configuration
  3. Configurations can also be added by clicking the Manage Configurations button in the Properties dialog. This brings up the Project Configuration Manager dialog which contains options for adding, removing, or duplicating configurations.

Customizing a Configuration

You can add as many configurations as you would like to your project. You can then modify settings in the Project Properties dialog for each configuration that you've added.

  1. Right-click the MyHello Project node and choose Properties. In the Properties dialog, choose the Platform node. This opens the J2ME MIDP Configuration dialog.
  2. Choose the BlackWhiteDisplay configuration from the configuration combo box at the top of the Properties dialog.
  3. The components in the Platform dialog are all disabled. This is because this configuration is currently taking the values used by the Default configuration for this panel. Uncheck the Use Default Project Configuration Values option at the bottom of the panel. All components on this panel are now editable.
  4. Change the Device option to DefaultGrayPhone.
  5. Toggle the Project Configuration dialog at the top of the Properties dialog and observe that the Device option changes based on which configuration is currently selected. This method of configuration customization works for each panel (other than the general panel) in the dialog. Click OK to save your configuration changes and exit the Properties dialog

Adding Configuration Specific Code

It is often useful to have code which is only included when you are building the JAR for a specific configuration (or group of configurations).

  1. The editor window for HelloMIDlet.java has a combo-box in the upper right corner which reads "Default Configuration" by default. Change this to your newly created configuration, BlackWhiteDisplay.
  2. Right-click on the source code line where TextBox is instantiated. Choose Preprocessor Blocks > Create If/Else Block. In the first, gray block, append the word "Gray" to the "Hello World" string. In the second, pink block, add "Color" to the "Hello World" string.
  3. Toggle the configuration combo box in the upper right corner of the editor and notice that blocks are commented in or out based on which configuration is selected. The first block will be commented in whenever the selected configuration matches the value in the header and footer of the block. The second block will be commented in whenever the selected configuration does not match the value in the header and footer. This combo box also controls which properties from the Project Properties are used when taking action on the project.

Running Configurations

Configurations can be built and run individually or simultaneously.

  1. Right-click the MyHello Project node and choose Run.
  2. Activate a different configuration by right-clicking the MyHello Project node and choosing Set Active Project Configuration and then a choose a different configuration. This can also be done with the configuration combo box in each text editor. Run the project again.
  3. Two emulators appear, one color and one gray. The gray emulator displays "Hello World Gray" and the color emulator displays "Hello World Color"

Localizing Your Application


Adding Localization Support

  1. Right-click the MyHello Project node and choose New > File/Folder.
  2. Select the MIDP folder, and select the Localization Support Class. Click Next.
  3. Accept the default values by clicking Finish. This creates the files LocalizationSupport.java and messages.properties.
  4. Use the editor to add your keys and their values into the messages.properties file. For example, "LAB_NextButton = Next".
  5. Enter the following string in your MIDlet wherever you want a key localized: LocalizationSupport.getMessage("Key");
    For example, LocalizationSupport.getMessage("LAB_NextButton");

Bookmark this page

del.icio.us furl simpy slashdot technorati digg

Sample Projects

Just want to play with some projects? In the IDE, choose File > New Project, then look under the Samples > Mobile Samples folder. The IDE includes many J2ME MIDP projects.

Related Documents

A short guide to getting your existing J2ME MIDP applications into NetBeans IDE 4.0.
Quickly create, build, and execute a simple J2SE application.
A short guide to getting your existing Java applications into NetBeans IDE 4.0 .
A complete list of docs for the NetBeans IDE.

 

Companion
Projects:
MySQL Database Server   Open JDK: an Open SourceJDK   GlassFish Community: an Open Source Application Server    Mobile & Embedded Community    Open Solaris   java.net - The Source for Java Technology Collaboration   Open ESB - The Open Enterprise Service Bus Powered by