07 - J2ME Graphic User interface: Page 4 of 6

TextBox Class

This class extends the Screen class and allows us to create screens with a text editor.

The TextBox class has a constructor like this:

TextBox (String title, String text, int size, int restrictions)

  1. The size will set how many characters are allowed in the text.
  2. The restrictions are some validations that the textbox will perform or allow on the entered text. The restrictions include: EMAILADDR, NUMERIC, PASSWORD, PHONENUMBER and URL among others.
package my.demo;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class HelloWorld extends MIDlet implements CommandListener {
    private Display myDisplay;
    private TextBox myTextBox = new TextBox("Password", "Password", 50, TextField.PASSWORD);
    private Command exitCommand = new Command("Exit", Command.EXIT,1);
    public HelloWorld() {
        myDisplay = Display.getDisplay(this);
        myTextBox.addCommand(exitCommand);
        myTextBox.setCommandListener(this);
    }
    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        notifyDestroyed();
    }
    protected void pauseApp() {
    }
    protected void startApp() throws MIDletStateChangeException {
        myDisplay.setCurrent(myTextBox);
    }
    public void commandAction(Command c, Displayable d) {
        if (c==exitCommand) {
                try {
                    destroyApp(false);
                } catch (MIDletStateChangeException e) {
                    e.printStackTrace();
                }            
        }
    }
}

When executing this MIDlet, our initial screen will show something like this:

          

Form Class

This class extends the Screen class, and it is a component that acts as a container for multiple items, and each and every item it can contain has to inherit from the Item class.

For dealing with changes in one of our objects that extends the Item class, we need to implement an ItemStateListener interface, which is very similar to the CommandListener we already used. We will see an example of this implementation with the TextField class.

StringItem Class

This class is the simplest class that extends Item. It will allow us to show an unmodifiable text string. It has two constructors:

StringItem (String label, String text)

StringItem (String label, String text, int appearance)

  1. The label is the label to be shown
  2. The text is the actual text to be displayed.
  3. The appearance is the text appearance, which can be: Item.PLAIN, Item.HYPERLINK or Item.Button.
package my.demo;
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.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class HelloWorld extends MIDlet implements CommandListener {
    private Display myDisplay;
    private Form  myForm= new Form("Form");
    private Command exitCommand = new Command("Exit", Command.EXIT,1);
    private StringItem myStringItem = new StringItem("MyLabel", "MyText");
    public HelloWorld() {
        myDisplay = Display.getDisplay(this);
        myForm.append(myStringItem);
        myForm.addCommand(exitCommand);
        myForm.setCommandListener(this);
    }
    protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
        notifyDestroyed();
    }
    protected void pauseApp() {
    }
    protected void startApp() throws MIDletStateChangeException {
        myDisplay.setCurrent(myForm);
    }
    public void commandAction(Command c, Displayable d) {
        if (c==exitCommand) {
                try {
                    destroyApp(false);
                } catch (MIDletStateChangeException e) {
                    e.printStackTrace();
                }            
        }
    }
}

When executing this MIDlet, our initial screen will show something like this:

          

Like us on Facebook