07 - J2ME Graphic User interface: Page 2 of 6

High Level User Interface

We will now see more details about the classes that inherit from Screen class; all of these classes are considered High Level user interface classes.

public abstract class Screen extends Displayable

Alert Class

This class extends the screen class and represents an alert screen, usually used for displaying a special message to the end user, like an error or a warning.

          public class Alert extends Screen

The Alert class has two constructors:

Alert (String title)

Alert (String title, String alertText, Image image, AlertType type)

  1. Title: sets the title for the alert screen
  2. alertText: can be used to add more information to be displayed within the alert
  3. image: allows you to display an specific image for the Alert.
  4. Type: it is an identifier for the type of Alert we want to create, the values are static finals from the AlertType class: ALARM, CONFIRMATION, ERROR, INFO and WARNING.

The Alert object has a method called: setTimeout that allows you to define how much time the alert will be shown. An special value shall be used when the alert is expecting an action from the user, and in that case you should set the value to Alert.FOREVER.

package my.demo;
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.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class HelloWorld extends MIDlet implements CommandListener {
    private Display myDisplay;
    private Form myForm, myOKForm;
    private Alert alertForEver, alertFiveSec;
    private Command exitCommand = new Command("Exit", Command.EXIT,1);
    private Command okCommand = new Command("OK", Command.OK,1);
    private Command alertForEverCmd = new Command("Alert Forever", Command.SCREEN,1);
    private Command alertFiveSecCmd = new Command("Alert 5 Sec", Command.SCREEN,1);
    public HelloWorld() {
        myDisplay = Display.getDisplay(this);
        myForm = new Form("Hello World!!!!");
        myOKForm = new Form ("OK");
        alertForEver = new Alert("Alert For Ever","Alarm will dissapear when you press the accept button",null,AlertType.INFO);
        alertFiveSec = new Alert("Alert Five Sec","Alarm will dissapear in five seconds",null,AlertType.INFO);
    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        myDisplay = null;
        myForm = null;
    protected void pauseApp() {
    protected void startApp() throws MIDletStateChangeException {
    public void commandAction(Command c, Displayable d) {
        if (c==exitCommand) {
                try {
                } catch (MIDletStateChangeException e) {
        } else if (c==okCommand) {
        } else if (c==alertForEverCmd) {
            myDisplay.setCurrent(alertForEver, myForm);
        } else if (c==alertFiveSecCmd) {
            myDisplay.setCurrent(alertFiveSec, myForm);

When executing this MIDlet, our initial screen will show something like this:


If you select the Menu option, you will be presented with multiple options, each represents one of the commands that we have in our initial form:


If you select the option 2, an Alert will be displayed for 5 seconds and then it will return automatically to the previous screen.


If you select the option 3, the alert will be displayed until the accept button is selected (or Done), once selected the previous screen will be displayed


Like us on Facebook