C H A P T E R 4 |
Creating a MIDlet and MIDlet Suite |
This section of the tutorial shows you how to create a currency converter application using the tools available to you through the Java Studio Mobility IDE. The chapter takes you through the tasks necessary to build the J2ME, MIDP/CLDC application, Currency Converter. The sections in this chapter are:
As you go through the tutorial, keep in mind that the Java Studio Mobility often has more than one way to perform a particular function. The tutorial illustrates one way to perform a function, but there are often other ways to accomplish the same function. For example, functions from the drop-down menus can usually be accessed by right-clicking on an entity and then selecting from its contextual menu. As you grow more familiar with the tool, you will find the operational mode with which you are most comfortable.
Before you create the Currency Converter MIDlet, you need to select a directory (filesystem) on your system, and mount it into the IDE. (Mounting is explained in Mounting a Filesystem.) Then you'll create a package, myconverter, to contain the Currency Converter files.
This tutorial will use a directory in the default Java Studio Mobility user directory for a user named JavaCoder on the Windows platform:
c:\Documents and Settings\JavaCoder\jstudio_6me_user\examples.
If the examples filesystem is already mounted, you can skip to Step 4.
To mount a filesystem and create the package:
1. From the File menu of the IDE, choose Mount Filesystem.
This opens a wizard from which you choose the template for the filesystem.
2. Select the Local Directory and click Next.
3. Use the wizard to navigate to the examples directory. Select this directory and click Finish to complete the mount process.
The examples filesystem appears in the Filesystems tab of the Explorer pane.
4. Right-click on the examples filesystem, then choose NewJava Package.
This opens the New Java Package wizard.
5. Name the package myconverter. Click Finish.
A package myconverter is created inside the mounted filesystem examples.
While you can work with individual MIDlets for developing and testing purposes, it is best to create MIDlets within a MIDlet suite. The MIDlet suite helps you to package your MIDlet application and prepare it for deployment.
MIDlet suites give you more control over your MIDP applications. A MIDlet suite organizes the source files and attribute files for a MIDP application. When you build a MIDlet suite, the tool automatically creates the necessary JAR file that contains the application files. The Java Studio Mobility IDE also creates the Java Application Descriptor file, or JAD file, that is required for deployment.
1. In the Filesystems window, right-click the myconverter package. Choose New MIDlet Suite from the contextual menu.
The MIDlet Suite wizard takes you through the steps to create a MIDlet suite.
2. In the MIDlet Suite wizard, type the name for the new MIDlet suite. Then click Next.
Name the new MIDlet suite converter.
3. In the Add MIDlet page, do the following to create a new MIDlet within the suite:
a. Select the Create New MIDlet option.
b. Enter ConverterMIDlet as the Package and Class Name.
Notice that you need to capitalize the "C" in the name.
c. To select a MIDlet Template, click the arrow in the dropdown list and choose MIDlet.
4. In the MIDlet Properties page, change the MIDlet Displayable Name to Currency Converter. Click Finish.
The displayable name is the name a mobile device user will see when using the application on a mobile device.
The code for the MIDlet is displayed in the Source Editor window. Notice that the ConverterMIDlet icon in the Explorer tab has a set of small red x's and 0's next to it. This badge signifies that the MIDlet needs to be compiled.
In the following steps, you will add code to complete the Currency Converter application.
You can write the code for a MIDlet in one of two ways: either by directly entering code in the Source Editor or by using the tool functions to add methods, fields, constructors, initializers, classes, and interfaces. Typically, you use the tool to add new fields and methods to a class, or modify existing fields and methods, and then later fine-tune the code directly in the Source Editor.
The following procedure shows you how to use the tool and the Source Editor to enter or change code. However, to save time and effort, you can also copy the converter code from the example you installed in Chapter 2.
1. In the Source Editor, add the following import statements to ConverterMIDlet:
2. In the Filesystem tab window, expand the converterMIDlet node, right-click the ConverterMIDlet class and choose Add Field.
This next step will use the Add New Field dialog box to add the field storedDataStr to the MIDlet. The storedDataStr string contains the name of the RMS stored record.
3. Complete the Add New Field dialog box:
a. Enter the name of the new field, storedDataStr, in the Name box and select its type, String, from the Type combo box.
b. In the Modifiers box, select the type of access for the field, private, from the Access combo box.
c. Check the other modifiers for the field, which in this case is static.
d. Set the initial value for storedDataStr to "ConverterData".
e. Click OK to close the dialog box.
The field is added to the code in the Source Editor window.
4. Add the following fields to the MIDlet code using the Source Editor.
5. Add the following code to the method startApp():
This method is called when the application is started. It loads all the data (currencies selected currencies, and exchange rates) from persistent storage and initially displays the Converter form.
6. Add the following code to complete the method destroyApp():
The destroyapp() method is called when the application is finished, or destroyed.
7. Add the following three new methods:
This method creates and displays the CurrenciesSelector list.
This method displays a new Converter form after the settings are changed.
public long convert(long frval, int fridx, int toidx) { return (frval * rates[fridx][toidx]) / 1000000; |
This method performs the currency conversion. The input value, frval, is multiplied by the exchange rate stored in the rates table and divided by 1,000,000. The fridx and toidx values are the indexes of the source and target currencies.
8. Save the ConverterMIDlet by choosing File Save.
Now that you have completed the code for the MIDlet, you will create the application's graphical interface. A Form is a Java class that can contain an arbitrary mixture of items, including images, read-only and editable text fields, editable date fields, gauges, choice groups, and custom items. The form you create here will specify a text box for each selected currency and specify the ItemStateListener() method to monitor and reflect typed values and perform conversions.
1. In the Filesystems window, right-click the myconverter package. Choose NewAll Templates.
The New Template wizard opens.
2. Expand the MIDP node and select MIDP Form. Click Next.
3. In the Object name page, Enter Converter for the class name. Click Finish.
A MIDP form template is created and added to the myconverter package.
4. In the Source Editor, add the following fields to the code below the public class Converter declaration:
5. Add the following code to replace the constructor:
6. Add the following code to complete the method commandAction():
if (command.getCommandType() == Command.EXIT) { midlet.destroyApp(true); } else if (command.getCommandType() == Command.OK) { midlet.showSettings(); } |
7. Add the following code to complete the itemStateChanged() method:
This completes the Converter.java form file.
The final piece of the Currency Converter application is the CurrenciesSelector.java list file, which defines the currencies that can be selected for display.
1. In the Filesystems window, right-click the myconverter package. Choose NewAll Templates.
The New Template wizard opens.
2. Expand the MIDP node and select MIDP List. Click Next.
3. In the New Object Name page, Enter CurrenciesSelector for the class name. Click Finish.
A MIDP list template is created and added to the currencyconverter filesystem.
5. Add the following code to replace the constructor
public CurrenciesSelector(ConverterMIDlet midlet):
6. Add the following code to complete the commandAction method:
if (command.getCommandType() == Command.OK) { getSelectedFlags(midlet.selected); midlet.notifySettingsChanged(); } |
7. To save the list, Select File from the main menu and choose Save.
This completes the CurrenciesSelector.java List file.
When you created the converter MIDlet suite, you created the essential package for the Currency Converter application. Now you should check to ensure that the two additional files you created, Converter.java and CurrenciesSelector.java have been added to the MIDlet suite. To do this, you use the Suite Editor.
To check the contents of the suite:
1. Right-click on the converter MIDlet suite and choose Edit Suite from the contextual menu.
The Suite Editor dialog opens. Notice that the Editor has several tabs: MIDlets, JAD Manifest, Push Registry, API Permissions, Signing, and Jar Contents.
2. Select the Jar Contents Tab.
The Jar contents tab displays, showing you the mounted file systems available, and the current contents of converter.jar. The current contents are marked with a check in the check box.
3. Expand the examples filesystem node.
4. Expand the myconverter folder.
Notice that the MIDlets are already selected. They were automatically added to the MIDlet Suite by the IDE.
If you left the "Include Whole Folder" option checked when you created the converter MIDlet in the New MIDlet Suite wizard (see Step 3 in Creating the MIDlet Suite), the converter example application, which also resides in the examples folder, is also included in the contents.
5. Make sure the converter folder is unchecked.
6. Click OK to close the dialog.
Now the suite is ready to be compiled and executed in the same way you compiled the MIDlet in Compiling and Running the Application.
The next chapter will illustrate how to debug a MIDlet suite with the Java Studio Mobility IDE.
Copyright © 2004, Sun Microsystems, Inc. All rights reserved.