Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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.

 

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

                         

» View all related tutorials
Related Tags: c j2me diff types type if example to rectangle draw exam raw angle e il pe in different m nt

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.