Exit Command : Command « J2ME « Java Tutorial






import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;

public class TextFieldMIDlet extends MIDlet implements CommandListener {

  private Command exitCommand = new Command("Exit", Command.EXIT, 1);

  private TextField aTextField = new TextField("Label", "Content", 4, TextField.ANY);

  private Display display = Display.getDisplay(this);

  public TextFieldMIDlet() {
  }

  public void startApp() {
    Form aForm = new Form("TextField");

    aForm.append(aTextField);
    aForm.addCommand(exitCommand);
    aForm.setCommandListener(this);
    display.setCurrent(aForm);
  }

  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
  }

  public void commandAction(Command c, Displayable s) {
    if (c == exitCommand) {
      destroyApp(false);
      notifyDestroyed();
    }
  }
}








31.17.Command
31.17.1.Add Command to MIDlet
31.17.2.Start the MIDlet by creating a list of items and associating the exit command with itStart the MIDlet by creating a list of items and associating the exit command with it
31.17.3.A MIDlet with simple text and a few commands.
31.17.4.Get command label and typeGet command label and type
31.17.5.Exit Command
31.17.6.Help commandHelp command