J2ME Command Class
In the given J2ME Command Class example, we have set the various commands
namely, screen, back, cancel, ok, help, stop, exit
and item
to the Midlet. And also set the priority for it such as 1, 2, 3
etc. In J2ME commands are used for keeping information of the commands, not the
action. For defining actions, CommandListener class are used. In this
example we are going to show different commands on the screen but we have not
used CommandListner class into it, that's these commands will not perform
any action.
Our command class Midlet will look like as follow..
Source Code of CommandMIDlet.java
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
public class CommandMIDlet extends MIDlet{
private Form form;
private Display display;
private Command screen, back, cancel, ok, help, stop, exit, item;
public CommandMIDlet(){
screen = new Command("Screen", Command.SCREEN, 1);
back = new Command("Back", Command.BACK, 2);
cancel = new Command("Cancel", Command.CANCEL, 3);
ok = new Command("Ok", Command.OK, 4);
help = new Command("Help", Command.HELP, 5);
stop = new Command("Stop", Command.STOP, 6);
exit = new Command("Exit", Command.EXIT, 7);
item = new Command("Item", Command.ITEM, 8);
}
public void startApp(){
display = Display.getDisplay(this);
Form form = new Form("Command Priority");
form.addCommand(screen);
form.addCommand(back);
form.addCommand(cancel);
form.addCommand(ok);
form.addCommand(help);
form.addCommand(stop);
form.addCommand(exit);
form.addCommand(item);
display.setCurrent(form);
}
public void pauseApp(){}
public void destroyApp(boolean destroy){
notifyDestroyed();
}
}
|
Download Source Code