Every J2ME application has an interface that enables user interactions with the application as follows:
- The UI may contain a single button or a group of radio buttons, check boxes or a list box on the form (container).
- Selections made by a user are considered events that are forwarded to your application by the device’s application manager for processing.
- The application’s developer must write code that recognizes an event and then reacts to the event by performing a task based on the nature of the application.
Three Kinds of User Interface
There are three kinds of user interfaces for an application. These are: a command, form, or canvas.

Command-based UI:
- A command-based user interface consists of instances of the Command class.
- An instance of the Command class is a button that the user presses on the device to enact a specific task.
- For example, Exit is an instance of the Command class associated with an Exit button on the key pad to terminate the application.
- The Help button is also an instance of the Command class that is linked to the Help key on the device, which is used whenever the user requires assistance
Form-based UI:
- A form-based user interface consists of an instance of the Form class that contains instances derived from the Item class such as text boxes, radio buttons, check boxes, lists to collect input from the user.
- A form is similar to an HTML form.
Canvas-based UI:
- A canvas-based user interface consists of instances of the Canvas class within which the developer creates images such as those used in a game.
Classes Used by J2ME
Display Class
- The device’s screen is referred to as the display. Each MIDlet has one and only one instance of the Display class
- We can interact with the display by obtaining the reference to an instance of the MIDlet’s Display class
- The instance of Display class is used to show instances of Displayable class on the screen.
private Display display;
display = Display.getDisplay(this);
- Multiple calls to the getDisplay(this) method return the same Display instance.
- Display class can be used to determine the color attribute of the small computing device’s screen.
Methods available in Display Class:
static Display getDisplay(MIDlet midlet) - Retreive an instance of the Display class
Displayable getCurrent() - Retrieve the current instance of Displayable class
void setCurrent(Alert alert, Displayable displayable) - Display the specified instance of the alert dialog box and then the specified instance of the Displayable class
void setCurrent(Displayable displayable) - Display the specified instance of the Displayable class
boolean isColor() - Determine whether the device supports color
int numColors() - Retrieve the number of colors or shades of gray that are available on the device
void callSerial(Runnable runnable) - Call an instance of the Runnable class after repainting.
Here is a presentation on Classes available in LCDUI package of J2ME.
ColorAttribute MIDlet:
- Display class is used to determine the color attribute of the small computing device’s screen.
- In this example, the MIDlet invokes the isColor() method of the Display class to determine whether the screeniscapableof displaying color.
- If so, the MIDlet displays an instance of the TextBox class reporting that the device has a color screen; otherwise the instance reports that the device is incapable of displaying color
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class CheckColor extends MIDlet implements CommandListener
{
private Display display;
private Form form;
private TextBox textbox;
private Command exit;
public CheckColor()
{
display = Display.getDisplay(this);
exit = new Command("Exit", Command.SCREEN, 1);
String message=null;
if (display.isColor())
{
message="Color display.";
}
else
{
message="No color display";
}
textbox = new TextBox("Check Colors", message, 17, 0);
textbox.addCommand(exit);
textbox.setCommandListener(this);
}
public void startApp()
{
display.setCurrent(textbox);
}
public void pauseApp()
{ }
public void destroyApp(boolean unconditional)
{ }
public void commandAction(Command command, Displayable displayable)
{
if (command == exit) { destroyApp(true);
notifyDestroyed();
}
}
}
Displayable Class
- There are two sub classes of Displayable class, namely Screen class and Canvas class.
- Instances of classes derived from the Displayable class are placed on the screen by calling the setCurrent() method of the Display class. The object that is to be displayed is passed to the setCurrent() method as a parameter.
Methods available in Displayable Class:
void addCommand(Command command) - Associate a command to an instance of the Displayable class
void removeCommand(Command command) - Disassociate a command from an instance of the Displayable class
void setCommandListener(CommandListener commandlistener) - Associate a CommandListener to an instance of the Displayable class
boolean isShown() - Determine whether an instance of the Displayable class is shown on the screen.