To define the MIDlets that should be included in the MIDlet suite, select the MIDlets tab. Initially, this contains a single row whose content is constructed from the name of the project. In this example, the suite contains a single MIDlet called ExampleMIDlet in the package ora.ch3, so you should press the Edit button and edit the values for the MIDlet-1 attribute on this tab so that it looks like this:

Key

Name

Icon

Class

MIDlet-1

ExampleMIDlet

/ora/ch3/icon.png

ora.ch3.ExampleMIDlet

In this example, the name assigned to the MIDlet matches the class name (ignoring the package prefix), but this need not be the case. Notice also that although the class name is specified in the usual way, with the parts of the name separated by periods, the location of the icon is specified as a filename, in which the path components are separated by a "/" character. If an icon is present, an absolute pathname must be provided here. If the MIDlet does not have an associated icon, this field should be left blank.

For a MIDlet suite with more than one MIDlet, you add an extra line for each MIDlet. It is important that consecutive numbers are used in the key field, so the next MIDlet to be added in this example would need to have the key MIDlet-2. Other required class files must be included in the JAR, but they should not be included in the MIDlets list.

For this example, we are also going to use a user-defined attribute. A user-defined attribute is a private attribute that can be set in the manifest and/or the JAD; its value can be retrieved at runtime by any MIDlet in the MIDlet suite. These attributes provide a mechanism similar to the setting of system properties in J2SE and allow the operation of the MIDlet to be customized without the need to recompile source code. In this example, we'll use a user-defined attribute to specify the length of a timer. To set the value of the attribute, select the User Defined tab and press the Add button. In the dialog box that appears, supply the property name as Timer-Interval and press OK. This creates a new entry in the table on the User Defined tab. Click in the Value cell, and type the required value, which, in this case, should be 3000. The property name is case-sensitive and, to avoid confusion with reserved attribute names, should not begin with "MIDlet-". The property value is always a string that is interpreted by the MIDlet. In this case, it represents the timer interval in milliseconds, so the value given here results in a timer that has a three-second interval. You'll see shortly how the MIDlet retrieves the values of user-defined attributes.

This completes the setting of the MIDlet's attributes. To save them, press the OK button at the bottom of the dialog. You can change these settings (perhaps to add extra MIDlets) at any time by pressing the Settings . . . button on the main KToolbar window, which is shown in Figure 3-7.

Figure 3-7. The main window of the Wireless Tooklit KToolbar

The next step is to place the source code and the icon for the MIDlet where the Wireless Toolkit can get access to them. Most IDEs allow you to choose where your project source files are kept, but the Wireless Toolkit uses a fixed filesystem layout for each project, based beneath the directory in which the Toolkit was originally installed. The name of the top-level directory for a project is derived from the name given to the project when it was created. If, for example, you installed the Windows Toolkit in the directory c:\J2MEWTK, all the files for the Chapter3 project need to be placed below the directory c:\J2MEWTK\apps\Chapter3. When the Chapter3 project was created, the toolkit created the following four directories below the main directory for the project:

src

Holds the source code for the MIDlets and any shared classes

res

Holds any resources required by the MIDlets, such as icons

lib

Holds JAR or ZIP files for third-party libraries that the MIDlets need

bin

Holds the JAR, JAD and manifest files

Before building the project, you need to place the appropriate files in the src, res and lib subdirectories. This example has one source file and a single icon, which can both be found in the directory ora\ch3 of the source code for this book. The package structure used by the MIDlet must be reflected in the directory layout as seen by the Wireless Toolkit, as it would be by an IDE. Therefore, to install the files where the Wireless Toolkit can use them, you should copy them as follows, creating the ora\ch3 subdirectory beneath both the src and res directories while doing so:

Source

Destination

ora\ch3\ExampleMIDlet.java

c:\J2MEWTK\apps\Chapter3\src\ora\ch3\ExampleMIDet.java

ora\ch3\icon.png

c:\J2MEWTK\apps\Chapter3\res\ora\ch3\icon.png

Once the files have been placed in the correct directories, the next step is to build the project by pressing the Build button on the KToolbar main window. The build process performs the following steps:

Creates a tmpclasses directory below the main directory, compiles all the source files below the src directory, and places the class files beneath tmpclasses, having regard to the package hierarchy. Thus, for example, the class files for the MIDlet ora.ch3.ExampleMIDlet would be placed in the directory c:\J2MEWTK\apps\Chapter3\tmpclasses\ora\ch3.

Creates a classes directory below the main directory and runs the preverifier on all of the classes found below tmpclasses, placing the verified class files below the classes directory in a directory layout that again reflects the package hierarchy. The verified ExampleMIDlet classes would, therefore, end up in c:\J2MEWTK\apps\Chapter3\classes\ora\ch3.

Creates the manifest file and the JAD file and places them in the bin directory.

TIP:
The source code for this book is actually stored in two different directory hierarchies, one for standard IDEs, the other for the J2ME Wireless Toolkit. This example showed you how to create a project from scratch using existing source files. A quicker way to use the book's source code is to copy the content of the directory wtksrc into c:\J2MEWTK\apps. This will give you subdirectories called Chapter3, Chapter4, etc., that contain all the source code and resources for each chapter's examples in the format expected by the J2ME Wireless Toolkit. To use each set of examples, select Open Project on the KToolBar main screen instead of Create Project, and then select the project from the dialog box that appears.