Java[tm] 2 Platform, Micro Edition (J2ME[tm]):
GUI API Application Design Using the Form Class and Items Components
by Allen Lai
(May 2002)
We want to hear from you! Please send us your
FEEDBACK.
The following code sample may contain actual software programs in source
code form. This source code is made available for developers to use as needed,
pursuant to the terms and conditions of this license.
SECTION A - Short Description/Synopsis
Synopsis: This code sample shows how to create a high level graphical user interface
(GUI) API application design using Form and Items.
One of the most common development requirements for a J2ME[tm] software application
is the need for high level graphical user interface applications, such as
providing a simple form on the screen to a J2ME[tm] device user for input.
Usually such application developers are also looking for an API that creates
GUI applications that are portable across most mobile information devices.
The CLDC/MIDP 1.0 specification provides a set of high level GUI APIs
through the Form class (which extends Screen) and Items, which are user
interface components that are somewhat closely related. These Items components
are used in combination within a Form to create a simple and functional
GUI application.
This code sample document demonstrates the usage of all the Items in
a single Form object in its most basic form. The program code imports the
javax.microedition.lcdui.* packages and is designed to add all the Items
components arbitarily.
SECTION B - Task and Skeleton Code
I. Task List
1. Starting with the skeleton code, first import the relevant J2ME
software packages for Form and Items.
2. Create the variables by declaring them before the startApp()
method.
3. Starting from within the startApp() method, create the Form
object first.
4. Use the StringItem() method to create a text string and attach
it to the form.
5. Use the Image() and ImageItem() method to create the images
respectively, and attach them to the form.
6. Use the TextField() method to create a text field box for text
string input and attach it to the form.
7. Use the DateField() method to display the date and time and
attach it to the form.
8. Use the Gauge() method to create graphics to display a range
of values and attach it to the form.
9. Use the ChoiceGroup() method and set it to the Exclusive mode
for single selection option and attach it to the form.
10. Use the ChoiceGroup() method and set it the Multiple mode
for multiple selection option and attach it to the form.
11. Add the command actions to the form for exiting the application.
12. Set the form to be the default screen to be displayed.
II. Skeleton Code
//import statements
import javax.microedition.midlet.* ;
import javax.microedition.lcdui.* ;
import java.util.* ;
public class ItemsinForm extends MIDlet implements CommandListener {
// variable declarations.
public void startApp() {
// Set the display.
// Create the Form object
for the Items to be attached to it.
// Create a StringItem (for
static strings and text).
// Create a Image and ImageItem
(for displaying images). A try-catch block must be used.
// Create a TextField (for
text field input).
// Create a DateField (for
displaying date and time).
// Create a Gauge (for displaying
values in a graphical manner).
// Create a ChoiceGroup (for
single selection) in the Exclusive mode.
// Create a ChoiceGroup (for
multiple selection) in the Multiple Choice mode.
// Add command and listener
to the Exit button.
// Set the Form object created
with all the Items appended to it as the current displayable object on
the screen.
}
public void pauseApp() {
// Print out on the System.out
console of the emulator or device text strings to indicate where the code
is running currently
}
public void destroyApp() {
// Print out on the System.out
console of the emulator or device text strings to indicate where the code
is running currently
}
public void commandAction (Command c, Display disp)
{
// place an Exit
option on the screen to exit the MIDlet.
}
}
SECTION C - Solution Source
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
public class ItemsinForm extends MIDlet implements CommandListener {
private final Command exit
= new Command("Exit", Command.OK, 1);
private Form form;
private Display display;
private ImageItem imageItem;
public void startApp() {
System.out.println("In startApp .....");
display = Display.getDisplay(this);
//Create the form with a title
form = new Form("Learning Forms and Items");
//String and StringItem()
form.append(new StringItem(null, "This is a StringItem"));
//Image and ImageItem
try {
Image image = Image.createImage("/Icon.png");
imageItem = new ImageItem("ImageItem is below:", image, ImageItem.LAYOUT_NEWLINE_BEFORE,
"Alternate text for the image file");
form.append(image);
form.append(imageItem);
}
catch (java.io.IOException e) {}
//TextField()
TextField textItem = new TextField("This is the TextField Title:", "This
is the TextField item", 30, TextFi
eld.ANY);
form.append(textItem);
//DateField()
DateField date = new DateField("The Date and Time now is:", DateField.DATE_TIME);
date.setDate(new Date());
form.append(date);
//Gauge()
Gauge gauge = new Gauge("Gauge level:", true, 10, 5);
form.append(gauge);
//ChoiceGroup() - Exclusive choice mode
ChoiceGroup excl_choose = new ChoiceGroup("Exclusive Options:", Choice.EXCLUSIVE);
excl_choose.append("Option 1", null);
excl_choose.append("Option 2", null);
excl_choose.append("Option 3", null);
form.append(excl_choose);
//ChoiceGroup() - Multiple choice mode
ChoiceGroup multi_choose = new ChoiceGroup("Multiple Options:", Choice.MULTIPLE);
multi_choose.append("Option A", null);
multi_choose.append("Option B", null);
multi_choose.append("Option C", null);
form.append(multi_choose);
//Add commands to buttons
form.addCommand(exit);
form.setCommandListener(this);
display.setCurrent(form);
}
public void pauseApp() {
System.out.println("In pauseApp .....");
}
public void destroyApp(boolean
unconditional) {
System.out.println("In destroyApp .....");
}
public void commandAction(Command
c, Displayable disp) {
if (c == exit) {
destroyApp(false);
notifyDestroyed();
}
}
}
Doc ID #650
|