Tuesday, May 4, 2010

J2ME - Form - Adding Items to Form

J2ME Forms can show different controls. For example you can show different labels, text fields for data entry, choice boxes and so on. We will address these fields in detail in our later posts but today lets see how to add simple fields to J2ME Form.

Following code will run and show a Form on your phone screen that will show three different items. First item is a plan String. Second item is a StringItem which is different from plain text string in a way that it has a title and a body. Different phones show different output for StringItem. The third Item added to the phone screen is a TextField. TextField is used for data entry. It has a label and a related data input box where you can type in information. Following is the code for the program:

        Form f = new Form("Form Title");

        StringItem label = new StringItem("User ID", "119");
        TextField login = new TextField("Login", "", 30, TextField.ANY);

        f.append("This is a simple string");
        f.append(label);
        f.append(login);
  
        display.setCurrent(f);


First line instantiates the Form and the only argument will work as the title of the Form. Second line instantiates a StringItem that will show a labeled String on the Form. Two arguments to the StringItem variable are label and string itself respectively. Third line is instantiating the TextField that will show a labeled text input box. You can type into this box for giving inputs. Argument to the TextField is the label of the box, second argument is the default text, if any (blank in our code's case) , third argument is the maximum number of characters allowed to be typed into this box and the last argument TextField.ANY is the input constraint. We will talk about TextField constrains later, for now you can type ANY text here.

Last four lines are simply calling append() method, that will add things to the form and showing the Form onto the phone screen. Following is the output of the program:


Following is the code for the whole program:

 package hello;

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

public class HelloMIDlet extends MIDlet implements CommandListener {

    private Command exitCommand; // The exit command
    private Display display;     // The display for this MIDlet

    public HelloMIDlet() {
        display = Display.getDisplay(this);
        exitCommand = new Command("Exit", Command.EXIT, 0);
    }

    public void startApp() {
        Form f = new Form("Form Title");

        f.addCommand(exitCommand);
        f.setCommandListener(this);

        StringItem label = new StringItem("User ID", "119");
        TextField login = new TextField("Login", "", 30, TextField.ANY);


        f.append("This is a simple string");
        f.append(label);
        f.append(login);
       

        display.setCurrent(f);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable s) {
        if (c == exitCommand) {
            destroyApp(false);
            notifyDestroyed();
        }
    }

}