The following list gives a line-by-line analysis of the program HelloMIDlet.java in Figure 11.4 (ref.: Java Me Programming Steps).
The package javax.microedition.lcdui provides a set of features for implementing user interfaces for MIDP applications. The class hierarchy is
- java.lang.Object
- javax.microedition.lcdui.AlertType
- javax.microedition.lcdui.Command
- javax.microedition.lcdui.Display
- javax.microedition.lcdui.Displayable
- javax.microedition.lcdui.Canvas
- javax.microedition.lcdui.Screen
- javax.microedition.lcdui.Alert
- javax.microedition.lcdui.Form
- javax.microedition.lcdui.List
(implements javax.microedition.lcdui.Choice)(implements javax.microedition.lcdui.Choice)
- javax.microedition.lcdui.TextBox
- javax.microedition.lcdui.Font
- javax.microedition.lcdui.Graphics
- javax.microedition.lcdui.Image
- javax.microedition.lcdui.Item
- javax.microedition.lcdui.ChoiceGroup
- javax.microedition.lcdui.DateField
- javax.microedition.lcdui.Gauge
- javax.microedition.lcdui.ImageItem
- javax.microedition.lcdui.StringItem
- javax.microedition.lcdui.TextField
- javax.microedition.lcdui.Ticker
The interface hierarchy is
- javax.microedition.lcdui.Choice
- javax.microedition.lcdui.CommandListener
- javax.microedition.lcdui.ItemStateListener
The package javax.microedition.midlet defines MIDP applications and the interactions between the application and the environment in which the application runs. The class hierarchy is
- java.lang.Object
- javax.microedition.midlet.MIDlet
- java.lang.Throwable
- java.lang.Exception
- javax.microedition.midlet.MIDletStateChangeException
The javax.microedition.midlet.MIDlet is an MIDP application. The application must extend this class to allow the application management software to control the MIDlet and to be able to retrieve properties from the application descriptor and notify and request state changes. The interface javax.microedition.lcdui.CommandListener is used by applications that need to receive high-level events from the implementation. An application will provide an implementation of a Listener and will then provide an instance of it on a Screen in order to receive high-level events on that screen.
The javax.microedition.lcdui.Display represents the manager of the display and input devices of the system. It includes methods for retrieving the properties of the device and for requesting the objects to be displayed.
The javax.microedition.lcdui.TextBox is a Screen that allows the user to enter and edit text.
The javax.microedition.lcdui.Command is a construct that encapsulates the semantic information of an action.
The purpose of a constructor is to initialize the instance variables of a newly instantiated object. The J2ME Application Manager (JAM) is responsible for installing, running, and removing MIDlets from the handheld devices. When a user chooses to run a MIDlet, it is the JAM that creates an instance of the MIDlet class and runs methods on it. The sequence of methods that will be called in the MIDlet subclass is defined by the MIDlet life cycle, which is shown in Figure 11.6.
The application manager calls methods in the MIDlet to signify changes from one state to another. startApp( ), pauseApp( ), and destroyApp( ) are the three life-cycle methods that every MIDlet must define.
A MIDlet goes through the following states:
When the MIDlet is about to be run, an instance is created. The MIDlet's constructor is run, and the MIDlet is in the Paused state.
Next, the MIDlet enters the Active state after the application manager calls startApp( ).
While the MIDlet is Active, the JAM can suspend its execution by calling pauseApp( ), which puts the MIDlet back in the Paused state.
The application manager can terminate the execution of the MIDlet by calling destroyApp( ), at which point the MIDlet is Destroyed and patiently awaits garbage collection.
The method public static Display javax.microedition.lcdui.Display.getDisplay( MIDlet m ) gets the Display object that is unique to this MIDlet.
The constructor public javax.microedition.lcdui.Command(String label, int commandType, int priority) creates a new command object with the following parameters:
label—The label string is what the application requests be shown to the user to represent this command.
commandType—The command types include BACK, CANCEL, EXIT, HELP, ITEM, OK, SCREEN, or STOP.public static final int SCREENspecifies an application-defined command that pertains to the current screen
priority—The application uses the value to describe the importance of this command relative to other commands on the same screen.
tbxMain = new TextBox( "HelloMIDlet","Hello, World!",50, 0);
The constructor public javax.microedition.lcdui.TextBox(String title, String text, int maxSize, int constraints) creates a new TextBox object with the following parameters:
title—The title text is to be shown with the display.
text—The initial contents of the text editing area. If the text parameter is null, the TextBox is created empty.
maxSize—The maximum capacity in characters, which must be greater than zero.
constraints—The different constraints allow the application to request that the user's input be restricted in a variety of ways. Constant 0 is assigned to public static final int ANY, which allows users to enter any text. Other constraints can be found from the Field Summary of input constraints.
The method public void javax.microedition.lcdui.Displayable.addCommand( Command cmd ) adds a command to the Displayable. The implementation may choose, for example, to add the command to any of the available soft buttons or place it in a menu.
The method public void javax.microedition.lcdui.Displayable.setCommandListener( CommandListener l ) sets a listener for Commands to this Displayable, replacing any previous CommandListener.
The MIDlet enters the Active state after the application manager calls startApp( ) via clicking on the button Launch.
When a MIDlet application is first started, there is no current Displayable object. It is the responsibility of the application to ensure that a Displayable is visible and can interact with the user at all times. Thus, the application should always call setCurrent( ) as part of its initialization. The method public void javax.microedition.lcdui.Display.setCurrent (Displayable next-Displayable) requests that a different Displayable object be made visible on the display.
These two methods usually have empty bodies.
This method implements the method public void javax.microedition.lcdui.CommandListener.commandAction(Command c, Displayable d), which indicates that a command event has occurred on Displayable d.
This checks to see if the Exit command was selected.
A MIDlet can destroy itself by calling the method destroyApp( ), which is empty in this case.
The method public final void javax.microedition.midlet.MIDlet.notifyDestroyed( ) notifies the application management software that it has entered into the DestroyeddestroyApp method, and all resources held by the MIDlet will be considered eligible for reclamation. The MIDlet must have performed the same operations (clean up, releasing of resources, etc.) as it would have if the MIDlet.destroyApp( ) had been called. state. The JAM software will not call the MIDlet's