Part 3: Event Handling – J2ME SDK 3.0 for Windows

Posted on: July 21, 2010



When a user interacts with a MIDlet loaded on a mobile information device, some events are generated. For example, if the user selects Save from the File menu, the application is notified of this action and responds to the generated event.

But how does the application get notified of an event, and how does it handle it?

Ans: There are two MIDP user interface APIs: high-level and low-level. Therefore, there are two kinds of events: high-level (such as selecting an item from a list) and low-level (such as pressing a key on the device).

Basics: Callbacks

When a user interacts with a MIDlet, events are generated and the application is modified to handle and respond to these events. The application is notified of such events through callbacks.

Thus, Callbacks are invocation of programmer-defined methods that are executed by the application in response to actions taken by a user at run time.

Callbacks are used in many programming environments, For example, the Abstract Window Toolkit (AWT) API makes heavy use of callbacks. When a user interacts with a component, for example, the interface code calls back the computational code to respond to the user’s action. Thus, when a user selects an item from a pull-down menu, the interface code for the menu will call back the computational code to invoke the selected item from the menu.

There are four kinds of user interface callbacks in MIDP:

  • Abstract commands that are part of the high-level API.
  • Low-level events that represent single key presses and releases.
  • Calls to the paint() method of a Canvas class.
  • Calls to a Runnable object’s run method requested by a call to the callSerially method of the Display class.

Screen Navigation

As a MIDlet developer, you are responsible for providing a way for the user to navigate the different screens that make up your MIDlet. The javax.microedition.lcdui package provides the Command class.

The Command class encapsulates the semantic information of an action. The command itself contains only information about a command, but not the actual action that happens when a command is activated. The action is defined in a CommandListener object associated with the screen.

Handling High-Level Events

Handling events in the high-level API is based on a listener model. Screen and Canvas objects can have listeners for commands. For an object to be a listener, it must implement the CommandListener interface.

You add commands to a displayable object with the addCommand method, and register a listener with the setCommandListener method. Both of these methods are part of the Displayable class and they are inherited by Screen and Canvas.

Example: Handling High-Level Events

A list of items  is created. The prepare() method is called whenever an item is selected. The testItem# methods, where # is a number between 1 and 4, call the prepare method and set the name of the menu.

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class EventEx1 extends MIDlet
implements CommandListener {
// display manager
Display display = null;

// a menu with items
List menu = null; // main menu

// textbox
TextBox input = null;

// command
static final Command backCommand =
new Command("Back", Command.BACK, 0);
static final Command mainMenuCommand =
new Command("Main", Command.SCREEN, 1);
static final Command exitCommand =
new Command("Exit", Command.STOP, 2);
String currentMenu = null;

// constructor.
public EventEx1() {

* Start the MIDlet by creating a list of items
* and associating the exit command with the list.
public void startApp()
throws MIDletStateChangeException {
display = Display.getDisplay(this);

menu = new List("Menu Items", Choice.IMPLICIT);
menu.append("Item1", null);
menu.append("Item2", null);
menu.append("Item3", null);
      menu.append("Item4", null);


public void pauseApp() {
display = null;
menu = null;
input = null;

public void destroyApp(boolean unconditional) {

// main menu
void mainMenu() {
currentMenu = "Main";

* a generic method that is called when any of
* the items on the list are selected.
public void prepare() {
input = new TextBox("Enter some text: ",
"", 5, TextField.ANY);

* Test item1.
public void testItem1() {
currentMenu = "item1";

* Test item2.
public void testItem2() {
currentMenu = "item2";

* Test item3.
public void testItem3() {
currentMenu = "item3";

* Test item4.
public void testItem4() {
currentMenu = "item4";

* Handle events.
public void commandAction(Command c, Displayable d) {
String label = c.getLabel();
if (label.equals("Exit")) {
} else if (label.equals("Back")) {
if(currentMenu.equals("item1") ||
currentMenu.equals("item2") ||
currentMenu.equals("item3") ||
currentMenu.equals("item4"))  {
// go back to menu
} else {
List down = (List)display.getCurrent();
switch(down.getSelectedIndex()) {
case 0: testItem1();break;
case 1: testItem2();break;
case 2: testItem3();break;
case 3: testItem4();break;


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 50 other followers

What I am Upto Now

Follow Me On Twitter

Blog Stats

  • 78,395 hits

Live Traffic

My Paintings

%d bloggers like this: