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();
}
}
}
Showing posts with label Form. Show all posts
Showing posts with label Form. Show all posts
Tuesday, May 4, 2010
Sunday, March 14, 2010
J2ME - Form - Code Example
This example will show you how simple it is to make J2ME Forms. All you have to do is create a MIDlet and in the startApp() method instantiate a Form and show a "Hello" string on top of it. If you have the development environment properly set up, this should take a few moments.
As you have read in the previous articles about the MIDlet life-cycle, startApp() is the method that is called by the J2ME runtime on the device and you can write your initializing code in this MIDlet method.
Following is a simple code chunk that will show a Form with a String on it.
How to create a Form in J2ME
In this code, on line 1 we create an instance of the Form. "Form Title" is obviously the text that will show as the title of the form on the screen. On line 2, we append a string to be shown on the form and the last line is making the Form visible on the screen.
Form f = new Form("Form Title");
f.append("Hello - This is the form");
Display.getDisplay(this).setCurrent(f);
Here is the full code MIDlet Example
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class FormExample extends MIDlet {
protected void startApp() throws MIDletStateChangeException {
Form f = new Form("Form Title");
f.append("Hello - This is the form");
Display.getDisplay(this).setCurrent(f);
}
public FormExample() {
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
}
Output of the above code:

Related Articles:
As you have read in the previous articles about the MIDlet life-cycle, startApp() is the method that is called by the J2ME runtime on the device and you can write your initializing code in this MIDlet method.
Following is a simple code chunk that will show a Form with a String on it.
How to create a Form in J2ME
In this code, on line 1 we create an instance of the Form. "Form Title" is obviously the text that will show as the title of the form on the screen. On line 2, we append a string to be shown on the form and the last line is making the Form visible on the screen.
Form f = new Form("Form Title");
f.append("Hello - This is the form");
Display.getDisplay(this).setCurrent(f);
Here is the full code MIDlet Example
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class FormExample extends MIDlet {
protected void startApp() throws MIDletStateChangeException {
Form f = new Form("Form Title");
f.append("Hello - This is the form");
Display.getDisplay(this).setCurrent(f);
}
public FormExample() {
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
}
Output of the above code:

Related Articles:
- J2ME Screens and User Interfaces
- J2ME - Alert - Code Example
- J2ME - Alert - How to show images in alerts
- J2ME - Alert - What are the alert types
Labels:
Code Example,
Displayable,
Form,
J2ME,
Java,
User Interfaces
Subscribe to:
Posts (Atom)