Two alerts : Alert « J2ME « Java Tutorial






Two alerts
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;

public class J2METwoAlerts extends MIDlet implements CommandListener {
  private Display mDisplay;

  private TextBox mTextBox= new TextBox("TwoAlerts", "", 32, TextField.ANY);

  private Alert mTimedAlert = new Alert("Network error", "error occurred", null, AlertType.INFO);

  private Alert mModalAlert= new Alert("About TwoAlerts","Alerts.", null, AlertType.INFO);

  private Command mAboutCommand, mGoCommand, mExitCommand;

  public J2METwoAlerts() {
    mAboutCommand = new Command("About", Command.SCREEN, 1);
    mGoCommand = new Command("Go", Command.SCREEN, 1);
    mExitCommand = new Command("Exit", Command.EXIT, 2);

    mTextBox.addCommand(mAboutCommand);
    mTextBox.addCommand(mGoCommand);
    mTextBox.addCommand(mExitCommand);
    mTextBox.setCommandListener(this);

    mModalAlert.setTimeout(Alert.FOREVER);
  }

  public void startApp() {
    mDisplay = Display.getDisplay(this);

    mDisplay.setCurrent(mTextBox);
  }

  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
  }

  public void commandAction(Command c, Displayable s) {
    if (c == mAboutCommand)
      mDisplay.setCurrent(mModalAlert);
    else if (c == mGoCommand)
      mDisplay.setCurrent(mTimedAlert, mTextBox);
    else if (c == mExitCommand)
      notifyDestroyed();
  }
}








31.14.Alert
31.14.1.Two alertsTwo alerts
31.14.2.Alarm alert, confirmation alert, error alert, info alert, warning alertAlarm alert, confirmation alert, error alert, info alert, warning alert
31.14.3.Set alert time outSet alert time out