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
- Choose File > New Project (Ctrl-Shift-N). Under Categories, select
Mobile. Under Projects, select Mobile Application and click Next.
- 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.
- Check the Create HelloMIDlet check box. Click Next.
- Leave the J2ME Wireless Toolkit as the selected Target Platform.
- 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.
- Expand the MyHello project node and double-click on the HelloMIDlet.java
source code node. The source code displays in the Source Editor.
- In the
startApp() method, replace the "test string" code with the
text of your choice. For example, "Hello World."
Compiling and Running a Project
- 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.
- 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.
- 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.
- 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.
- Click the Device dropdown menu and choose QwertyDevice. Click OK.
- Run the application again, and the applicaton runs in the
QwertyDevice emulator.
Adding a new Emulator Platform
- Choose Tools > Java Platform Manager from the main toolbar.
- In the Java Platform Editor dialog, click the Add Platform button.
- 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.
- 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.
- Choose File > "MyHello" Properties.
- 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.
- 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.
- 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
- 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.
- Right-click the MyHello Project node and choose Properties. In
the Properties dialog, choose the Platform node. This opens the J2ME
MIDP Configuration dialog.
- Choose the BlackWhiteDisplay configuration from the configuration
combo box at the top of the Properties dialog.
- 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.
- Change the Device option to DefaultGrayPhone.
- 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).
- 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.
- 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.
- 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.
- Right-click the MyHello Project node and choose Run.
- 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.
- 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
- Right-click the MyHello Project node and choose New > File/Folder.
- Select the MIDP folder, and select the Localization Support Class.
Click Next.
- Accept the default values by clicking Finish. This creates the files
LocalizationSupport.java and messages.properties.
- Use the editor to add your keys and their values into the messages.properties
file. For example,
"LAB_NextButton = Next".
- Enter the following string in your MIDlet wherever you want a key localized:
LocalizationSupport.getMessage("Key");
For example, LocalizationSupport.getMessage("LAB_NextButton");