PAGES

Friday, October 1, 2010

Working with multiple display pages in J2ME

Till now we have worked with only single page on which we kept on changing the display using setCurrent property.
Now we will learn how to deal with a seperate java code file.


For this we will create a simple midlet "Midlet1" on which we will display a question with four answers ,each having a checkbox in front of it.
Then we will have a lock button which locks the selected answer.

Now we create a second .java code file(not a midlet) since for a single apllication ,1 midlet is sufficient.
In that java file we take the  object of midlet as arguement so that we can return back to midlet when required.

Then we define GUI of java page by adding LCDUI package and others required for our purpose.
This page displays the answer using stringitem.
We also add a back button to get back to original page.
To start with this tutorial you need to go through all previous tutorials .     First Tutorial


To view video tutorial online click here


To download click here


Sorce code:-
Midlet1.java


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


public class Midlet1 extends MIDlet implements CommandListener {


    private Form form1 =new Form("Question 1");
    private ChoiceGroup c1=new ChoiceGroup("Who is the best batsman",Choice.MULTIPLE);
    private Command Ok =new Command("Lock",Command.OK,1);
    private GUI child;
    public Midlet1()
    {
        c1.append("Brian Lara", null);
        c1.append("Sachin Tendulkar", null);
        c1.append("Ricky Ponting", null);
        form1.append(c1);
        form1.addCommand(Ok);
        form1.setCommandListener(this);
    }


    public void commandAction(Command c,Displayable d)
    {
        if(c==Ok)
        {
          child=new GUI(this);
        }
    }
    public void startApp() {
        Display.getDisplay(this).setCurrent(form1);
    }
    public void show()
    {
         Display.getDisplay(this).setCurrent(form1);
    }
    public void pauseApp() {
    }
    public void destroyApp(boolean unconditional) {
    }
}


GUI.java


import  javax.microedition.lcdui.*;


public class GUI implements CommandListener {


    public Midlet1 parent;
    public Form form2 =new Form("Answer");
    String s="Sachin Tendulkar";
    public StringItem s1;
    public Command back =new Command ("Back",Command.BACK,2);


     GUI(Midlet1 parent)
    {
        this.parent=parent;
        s1=new StringItem("Answer is",s);
        form2.append(s1);
        form2.addCommand(back);
        form2.setCommandListener(this);
        Display.getDisplay(parent).setCurrent(form2);
    }
    public void commandAction(Command c,Displayable d)
    {
        if(c==back)
        {
            parent.show();
            return;
        }
    }
}

No comments:

Post a Comment

customised by Vaibhav