Java Reference
In-Depth Information
Listing 5-1.
Demonstrating the Relationship Between Command and Displayable Objects
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class CommandExample extends MIDlet {
public void startApp() {
Displayable d = new TextBox("Demo", "", 20, TextField.ANY);
Command exit = new Command("Exit", Command.EXIT, 0);
Command help = new Command("Help", Command.HELP, 1);
Command stop = new Command("Stop", Command.STOP, 2);
d.addCommand(exit);
d.addCommand(help);
d.addCommand(stop);
d.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable s) {
notifyDestroyed();
}
});
Display.getDisplay(this).setCurrent(d);
}
public void pauseApp() { }
public void destroyApp(boolean unconditional) { }
}
The diagram in Figure 5-4 shows the class relationship for this application. At
MIDlet startup, the MIDlet creates a
TextBox
instance and the command instances.
Next, it adds each of the new commands to the
TextBox
; in turn, the
TextBox
instance
determines where and how the commands should appear based on the labels and pri-
orities. The MIDlet also creates an anonymous
CommandListener
that responds to
all
of
the generated commands. It does this using the
TextBox.setCommandListener
method.
Finally, the
Display
's notion of the current display is set to the
Displayable
, so that the
TextBox
will be displayed.