Wednesday, September 22, 2010

Textboxes,Labels and more events in J2ME

This post explains adding multiple command buttons in a mobile application ,adding textboxes and labels as well as basic idea of displayable class
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
Learning Command action in J2ME (Events)

To View online CLICK HERE
To download Click here

 Here is the source code:-

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

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 save=new Command("Save Name",Command.SCREEN,1);
    private Command Current=new Command("Show current form name",Command.SCREEN,1);
    private Command EXIT=new Command("EXIT",Command.EXIT,0);
    private Command BACK=new Command("Back",Command.BACK,0);
    private StringItem si;

    private TextField t1 =new TextField("Your Name","Enter Text here",15,TextField.ANY);
    String s;

    public Midlet1()
    {
        form1.addCommand(ok);
        form1.addCommand(save);
        form1.addCommand(Current);
        form1.addCommand(EXIT);
        form1.append(t1);
        form1.setCommandListener(this);
        form2.addCommand(Current);
        form2.addCommand(BACK);
        form2.setCommandListener(this);
    }

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

    public void commandAction(Command c,Displayable d)
    {
        if(c==ok)
        {
            show(1);
        }
        if(c==save)
        {
            s=t1.getString();
            si=new StringItem("Your Name",s);
            form2.append(si);
            show(1);
        }
        if(c==EXIT)
        {
            destroyApp(true);
            notifyDestroyed();
        }
        if(c==BACK)
        {
            show(0);
        }
        if(c==Current)
        {
            if(d==form1)
            {
                t1.setString("form1");
            }
            else
             si.setText("form2");
        }
    }
    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

No comments:

Post a Comment

customised by Vaibhav