[an error occurred while processing this directive]

J2ME Event Handling Example


In J2ME programming language, Event Handling are used to handle certain type of events that are generated at the time of loading MIDlet on the mobile screen. As you know in J2ME there are two MIDP user interface APIs and therefore, there are two kinds of events: high-level and low-level 

High level event like selecting an item from a list

And low level events like pressing a key on the device

This example is going to show you both type of events handling. 




Image 1

Event handling in J2ME

Image 2

Handling events



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

public class EventHandlingMIDlet extends MIDlet implements CommandListener {
  private Display display;
  private List list;
  private TextBox input;
  private Command back, main, exit;
  private String currentItem;
    public EventHandlingMIDlet() {
    back = new Command("Back", Command.BACK, 0);
    main = new Command("Main", Command.SCREEN, 1);
    exit = new Command("Exit", Command.STOP, 2);
    public void startApp() throws MIDletStateChangeException {
    display = Display.getDisplay(this);
    list = new List("Menu Items", Choice.IMPLICIT);

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

  public void destroyApp(boolean unconditional) {

  void mainItem() {
    currentItem = "Main"

  public void show() {
    input = new TextBox("Enter Text Here: """10, TextField.ANY);

  public void commandAction(Command c, Displayable d) {
    String label = c.getLabel();
    if (label.equals("Exit")) {
    else if (label.equals("Back")) {
      if(currentItem.equals("Eventitem1"|| currentItem.equals("Eventitem2"|| 
("Eventitem3"|| currentItem.equals("Eventitem4")){
    else {
      List down = (List)display.getCurrent();
      switch(down.getSelectedIndex()) {
        case 0: showItem();break;
        case 1: showItem2();break;
        case 2: showItem3();break;
        case 3: showItem4();break;

  public void showItem() {
    currentItem = "Eventitem1";

  public void showItem2() {
    currentItem = "Eventitem2"

  public void showItem3() {
    currentItem = "Eventitem3"

  public void showItem4() {
    currentItem = "Eventitem4"

Download Source Code


[an error occurred while processing this directive]