当前页面: 开发资料首页 → Netbeans 专题 → NetBeans Mobility Pack 5.0 Quick Start Guide
NetBeans Mobility Pack 5.0 Quick Start Guide
摘要: This document takes you through the basics of using NetBeans IDE 5.0 to create a Java? Platform, Micro Edition (Java? ME platform), Mobile Information Device Profile (MIDP) application. This document is designed to get you going as quickly as possible. More information about using the Java ME support in the NetBeans development environment is provided in the following document
The following short tutorial takes you through some of the basic steps of working
with the new project system. We will show you two ways to create a Java ME MIDP
project named "MyHello" that displays the text "Hello World" in a device emulator.
Requirements
You must have NetBeans IDE 5.0 and the NetBeans Mobility Pack 5.0 installed
before you can start Java ME MIDP development. See the Mobility Pack Download Page
for instructions on downloading and installing
the complete environment.
Creating a MIDP Application Using the Visual Mobile Designer
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 Visual Mobile Designer gives you the ability to graphically plan
out the flow of the application and design the screens the application will
use. The designer automatically creates the code for the application.
Creating a MIDP Application
- Choose File > New Project (Ctrl-Shift-N). Under Categories, select
Mobile. Under Projects, select Mobile Application and click Next.
- Enter
MyHello
in the Project Name field. Change the Project
Location to any directory on your system. From now on, we will refer to
this directory as $PROJECTHOME
.
- Check the Set as Main Project and Create Hello MIDlet checkboxes (both
are checked by default). 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 application itself is displayed in the Flow Design window of the Visual
Mobile Designer.

Editing the Java Source Code
Now let's edit the text displayed by the MIDlet.
- Click on Screen Design.
This opens the Screen Designer window, and displays the helloForm[Form]
screen, which is the only screen available in the application.
- Double-click on the "Hello world!" text and type in some new text.
Compiling and Running the 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 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.
Creating a MIDP Application Using the Source Editor
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.
Now we'll create the same application using the New Project and New File wizards,
and completing the code using the Source Editor.
Creating a New Java ME MIDP project
- Choose File > New Project (Ctrl-Shift-N). Under Categories, select
Mobile. Under Projects, select Mobile Application and click Next.
- Enter
MyHelloMIDlet
in the Project Name field. Change the
Project Location to any directory on your system. From now on, we will refer
to this directory as $PROJECTHOME
.
- Check the Set as Main Project checkbox and remove the check from the Create
Hello MIDlet checkbox. Click Next.
- Leave the J2ME Wireless Toolkit as the selected Target Platform.
- Click Finish. The IDE creates the
$PROJECTHOME/MyHelloMIDlet
project folder. The project folder contains all of your sources and project
metadata, such as the project Ant script.
- Right-click the
MyHelloMIDlet
node in the Explorer window
and choose New > File/Folder.
- Under Categories, choose MIDP. Under File Types, choose MIDlet. Click
Next.
- Enter
HelloMIDlet
as the MIDlet name. Click Finish. The HelloMIDlet.java
is created.
- Double click the HelloMIDlet.java to display the source code in the Editor.
- Click in the Source Editor and change
public class HelloMidlet extends MIDlet
to
public class HelloMIDlet
extends MIDlet implements javax.microedition.lcdui.CommandListener
{
- Add the following text before the
startApp()
method:
public HelloMIDlet() {
}
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, "Test String",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;
- Add a line
initialize();
to the startApp()
method,
so it looks like the following:
public void startApp() {
initialize();
}
Editing the Java Source Code
Now let's add some text for our MIDlet to display.
- In the
get_helloTextBox()
method, replace the "test string"
code with the text of your choice. For example, "Hello World."
Compiling and Running the 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
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
Emulator platforms are provided by mobile phone manufacturers so developers
can simulate the behavior of specific mobile devices while coding, debugging,
and testing applications. The NetBeans Mobility Pack includes the J2ME Wireless
Toolkit, and supports many other software development kits (SDKs) that you can
download from provider sites, such as Nokia, Sony Ericsson, or Motorola.
Changing the Default Emulator Device
- Right-click the MyHello Project node and choose Properties. In the Properties
dialog, choose the Platform node. Here, you can change the device for the
default configuration.
- Click the Device drop-down list and choose QwertyDevice. Click OK.
- Run the application again, and the application runs in the
QwertyDevice
emulator.
Adding a new Emulator Platform
- Choose Tools > Java Platform Manager from the main toolbar.
- In the Java PlatformManager dialog, click the Add Platform button.
- In the Select Platform Type page, choose the Java Micro Edition Platform
Emulator. Click Next.
In the Platforms page, the IDE searches for all available Java ME platforms
and lists them in the window. Platforms that are compliant with the Unified
Emulator Interface (UEI) standard are verified and appear with a check in
the checkbox next to the platform name. Platforms that are not UEI-compliant,
or that you might not be able to install for other reasons, are listed in
red. The wizard also identifies which platforms have already been installed.
NOTE: To install platforms that are not UEI-compliant,
click the Back button and choose Custom Java Micro Edition Platform Emulator.
- In the Platforms page, put a check next to any platform emulator you want
to install. Click Finish. Then click Close.
The selected platform or platforms are installed.
- 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 drop-down
menu. Click OK.
Using Configurations
You can use configurations to create more than one set of distribution JAR
and Java Application Descriptor (JAD) files for your project. This enables you
to create one set of source code and customize it for each mobile device you're
programming for.
Adding a Configuration
- Right-click the MyHello Project node and choose Properties. In the Properties
dialog, Choose Add Configuration from the Project Configuration drop-down
list. 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
can now be edited.
- 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.
Creating an Ability
When you create an ability, you associate it with one or more project
configurations that support a specific attribute or set of attributes.
You can then associate the ability with one or more code blocks that support
the attribute. If you later need to add or remove configurations, you
won't have to go through all your code and associate the configuration
with each code block. Instead, you need only to associate the configuration
with an ability. The configuration is then automatically associated with
any code block that is associated with the ability.
- Right-click on the MyHello project node and choose Properties.
- Choose the Abilities page from the Category menu tree.
- Choose a configuration from the Project Configuration dropdown menu.
This is the configuration with which the ability will be associated.
- If necessary, uncheck the Use Values from "DefaultConfiguration" checkbox.
- Click the Add button.
This opens the Add Ability dialog.
- In the New Ability dialog, enter a name for the ability, or choose
an ability from the drop-down menu. Optionally, enter a value for the
ability.
Click Ok to close the New Ability dialog. The ability is now associated
with the selected project configuration.
You can associate the ability with other configurations by choosing a different
configuration from the Project Configuration menu and clicking the Add button.
Adding Configuration-Specific Code (Preprocessor
code)
Preprocessor blocks enable you to create, manage and track code that
is specific to one or more project configurations or abilities. The code
is enabled (or disabled) only for the configurations or abilities you
identify.
- In the HelloMIDlet.java Editor window, right-click on the source code
line where TextBox is instantiated (
helloTextBox = new javax.microedition.lcdui.TextBox(null,
"Hello, World",120, 0x0);
).
- Right-click on the line and choose Preprocessor Blocks > Create
If/Else Block. In the first, pink section, append the word "Color" to
the "Hello World" string. In the second, gray section, add "Gray" to
the "Hello World" string.
- The code marked by the
//#if
preprocessor directive will
be in effect whenever the active configuration matches the directive
value (in this case, DefaultConfiguration.) The code encased by the
//#else
preprocessor directive will be used whenever the
active configuration does not match the value of the
preprocessor.
In other words, the emulator displays "Hello World Color"
whenever the active platform is "DefaultConfiguration," and
displays "Hello World Gray" if the active platform is any
other emulator platform. The //#endif preprocessor directive completes
the preprocessor block.
You can test the preprocessor code by running the MIDlet using different
emulator devices, as described in Changing the
Emulator Platform.
Running Configurations
Configurations can be built and run individually or simultaneously.
- Right-click the MyHelloMIDlet Project node and choose Run.
- Activate a different configuration by right-clicking the MyHelloMIDlet
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"
Creating a Client-Server Application
The IDE enables you to create client MIDlets that can connect to Web services
either through a direct "two-tier" architecture, or a "three-tier"
architecture that connects to Web services through a middleware Web application.
The IDE provides two wizards for creating the connecting classes:
- Use the The J2ME Web Service Client wizard if you want to use the JSR-172
Web Services specification to create a "two-tier" connection to
Web services. This wizard creates stub (client-side proxy) files and, optionally,
a sample client MIDlet that calls a web service. You must have a device or
emulator platform that supports the JSR-172 specification to use this wizard.
- Use the The Mobile Client to Web Application Generator to create a "three-tier"
connection to Web services if your devices do not support the JSR-172 Web
Services specification. The wizard generates a servlet that connects to a
web application that includes a web service client. Before you use the Mobile
Client to Web Application Generator, you must have a Web project that includes
a Web service client.
Creating a Java ME Web Services
(JSR-172) Client
- Right-click on a project node and choose New > File/Folder.
- In the New File wizard, choose MIDP under Categories. Under File Types,
choose J2ME Web Services Client.
- Identify the WSDL file location for the Web service to be added to the
project.
- If the WSDL service is on the Web:
- Choose Running Web Service and enter a URL for the WSDL file and
enter a local filename for the retrieved WSDL.
- If you are behind a firewall, click Proxy Settings to designate
the appropriate HTTP host and port.
- Click Retrieve WSDL to get a local copy of the WDSL file. The IDE
downloads the WSDL file.
- If you have a WSDL file in your environment, select Existing WSDL file
and enter a filename.
- The WSDL file is retrieved and the rest of the fields on the page are
filled with default names derived from the WSDL file name.
- Press Finish.
The IDE creates an opened settings file and generates client stubs.
- In some rare situations, you might need to convert CLDC 1.1 types to
CLDC 1.0 types, which means Float and Double types are converted to String
variables. To make the conversion, select the checkbox “Convert
floating point to String” on the settings file and click the Generate
Stubs button.
- If you are behind a firewall, there is no public setting available for
the HTTPS proxy within NetBeans. To work around this, run the IDE from
the command line with the command
-J-DhttpsProxy.host and -J-DhttpsProxy.port
,
or add the proxy settings into the /etc/netbeans.conf
file.
- The generated files should not be edited. Any changes made to the files
are lost when you regenerate them.
Using the Mobile Client to Web Application Generator
- Open the Mobile Client to Web Application Generator by selecting a
Mobile Project from the Projects view and choosing File > New File.
In Categories, chose MIDP. In File Types, choose Mobile Client to Web
Application Generator Client. Click Next.
- In the Servlet and Client Type Selection page, select a web project,
servlet name, and package for the generated server code.
- In the same page, choose the type of web application the MIDlet interacts
with:
- If the mobile client connects directly to a Web application that
does not use a Web service, choose Methods in Web Application.
- If the mobile client connects to a Web service through a middleware
Web application, choose Web Services in Web Application, then use
the drop-down menu to choose the Web Service client.
- In the Service Selection page, identify the services to be exported
from the server to the client application.
- In the Client Options page, select a name and package for the generated
client class.
- In the same page, you can also determine the types of code to be generated:
- Stub methods for each exported service
- Code enabling multiple calls per session
- Code that allows floating point values
- Tracing code
- A sample MIDlet you can examine and modify
The template creates:
- a Java ME client class
- a servlet and supporting classes
- a mapping file in xml format.
- if you choose, a MIDlet you can examine and modify.
The Java ME client , mapping file and the MIDlet are generated under the
Java ME project. The servlet and supporting classes are generated under
the web project. The servlet is also added to the web application (web.xml
).
Localizing Your Application
Localization is the process of designing software so that
it can be adapted (localized) to various languages and regions without engineering
changes to the software. When you localize an application, you provide alternate
language values for each element in your application, such as screen text,
menus, and icons.
Adding Localization Support
- Create the java classes that initialize the localization support and message
property files.
- Right-click the MyHelloMIDlet Project node and choose New > File/Folder.
- Under Categories, select MIDP. Under File Types, select Localization
Support Class. Click Next.
- Accept the default values by clicking Finish. This creates the files
LocalizationSupport.java
and messages.properties
.
- Change the Internationalization String and Search formats.
- Choose Tools > Options and click the Advanced Options button.
- Select the Internationalization node in the Options menu tree.
- In the properties window:
- Change the value of the II8N String Format property to
LocalizationSupport.getMessage("{key}")
- Change the value of the II8N Search Format property to
(getString|getBundle|getMessage)[:space:]*\([:space:]*{hardString}|//NOI18N
- Add localization keys and their values.
- Choose Tools > Internationalization > Internationalization Wizard.
This opens the Internationalization wizard.
- Select the
HelloMIDlet
MIDlet as the source file to internationalize.
Click Next.
- Select the
messages.properties
resource
file as the file in which to store the values for the internationalized
strings. Click Next.
- Modify the Value fields for each key identified for the
HelloMIDlet
MIDlet.
- After all the strings have been added to the
messages.properties
file, do the following to add values for each new locale:
- Right-click on the file node in the Projects view and choose Add
Locale.
- Choose a locale you want to support from the Predefined Locales
list box, or use the combo boxes at the top of the form to define
a new locale.
- Expand the
message.properties
node in the Projects
view, and double-click on the newly-added locale.
- Translate all properties into the appropriate language.
- Add code to initialize localization.
- Enter the following string in your MIDlet wherever you want a key
localized:
LocalizationSupport.getMessage("Key");
For example, LocalizationSupport.getMessage("LAB_NextButton");
The mobile application will use the microedition.locale
attribute
to determine which version of the messages.properties
file should
be used.
Next Steps
For more information about using NetBeans IDE 5.0, see the following resources: