Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home J2me J2ME Event Handling Example

Related Tutorials


 
 

Share on Google+Share on Google+

J2ME Event Handling Example

Advertisement
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.

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

 

EventHandlingMIDlet.java

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);
  list.append("EventItem1", null);
  list.append("EventItem2", null);
  list.append("EventItem3", null);
  list.append("EventItem4", null);
  list.addCommand(exit);
  list.setCommandListener(this);
  mainItem();
  }

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

  public void destroyApp(boolean unconditional) {
  notifyDestroyed();
  }

  void mainItem() {
  display.setCurrent(list);
  currentItem = "Main"; 
  }

  public void show() {
  input = new TextBox("Enter Text Here: ", "", 10, TextField.ANY);
  input.addCommand(back);
  input.setCommandListener(this);
  input.setString("");
  display.setCurrent(input);
  }

  public void commandAction(Command c, Displayable d) {
  String label = c.getLabel();
  if (label.equals("Exit")) {
  destroyApp(true);
  } else if (label.equals("Back")) {
  if(currentItem.equals("Eventitem1") || currentItem.equals("Eventitem2") || 
   currentItem.equals("Eventitem3") || currentItem.equals("Eventitem4")){
  mainItem();
  } 
  } 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() {
  show();
  currentItem = "Eventitem1";
  }

  public void showItem2() {
  show();
  currentItem = "Eventitem2"; 
  }

  public void showItem3() {
  show();
  currentItem = "Eventitem3"; 
  }

  public void showItem4() {
  show();
  currentItem = "Eventitem4"; 
  }
} 

Download Source Code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 1, 2008

Related Tutorials

Discuss: J2ME Event Handling Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com