Monday, September 13, 2010

Learning Command action in J2ME (Events)

This post gives you general overview about command buttons in a mobile application as well as redirection from one screen to other within same midlet.
Note:-  Before viewing this video please go through the previous tutorials as well since this one is the extention of previous tutorials.
Starting with J2ME
Understanding basic midlet source code

To View tutorial video Online CLICK HERE

To download Click here

Source Code:-

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.*;

/**
 * @author vaibhav
 */
public class Midlet1 extends MIDlet implements CommandListener
{
    public static Form form1=new Form("Hello ,this is my first form");
    public static Form form2=new Form("new form");
    private Command ok=new Command("load",Command.SCREEN,1);
    private Command EXIT=new Command("EXIT",Command.EXIT,0);

    public Midlet1()
    {
        form1.addCommand(ok);
        form1.addCommand(EXIT);
        form1.setCommandListener(this);
    }

    public void startApp() {
        show(0);
    }

    public void commandAction(Command c,Displayable d)
    {
        if(c==ok)
        {
            show(1);
        }
        if(c==EXIT)
        {
            destroyApp(true);
            notifyDestroyed();
        }
    }
    public void show(int i)
    {
        if(i==0)
        {
        Display.getDisplay(this).setCurrent(form1);
        }
        if(i==1)
        {
        Display.getDisplay(this).setCurrent(form2);
        }
    }
    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }
}                                                                                                            Next Tutorial

2 comments:

  1. Excuse me, why you use static keyword in :
    public static Form form1=new Form("Hello ,this is my first form");
    public static Form form2=new Form("new form");
    ?

    ReplyDelete
  2. static variable is not attached to a particular object, but rather to the class as a whole. They are allocated when the class is loaded.
    Here we want the form to get updated as soon as any alteration is made by single user(since we are designing it for multiuser domain).However as beginners u can skip the static part.

    ReplyDelete

customised by Vaibhav