Java Reference
In-Depth Information
Form s and Item s
The most important subclass of Screen is the class Form . A Form can hold any number of Item s
such as StringItem s, TextField s, and ChoiceGroup s. Item s can be added to the Form using
the append() method.
The Item class itself is an abstract base class that cannot be instantiated. It provides a label that is a
common property of all subclasses. The label can be set and queried using the setLabel() and
getLabel() methods, respectively. The label is optional, and a null value indicates that the item
does not have a label. However, several widgets switch to separate screens for user interaction, where
the label is used as the title of the screen. In order to allow the user to keep track of the program state, it
is recommended that you provide a label at least for interactive items.
Item s can neither be placed freely nor can their size be set explicitly. Unfortunately, it is not possible
to implement Item subclasses with a custom appearance. The Form handles layout and scrolling
automatically. Table 3.1 provides an overview of all Item s available in MIDP.
Table 3.1. All Subclasses of Item
Item
Description
ChoiceGroup
Enables the selection of elements in group.
DateField
Used for editing date and time information.
Gauge
Displays a bar graph for integer values.
ImageItem
Used to control the layout of an Image .
StringItem
Used for read-only text elements.
TextField
Holds a single-line input field.
StringItem
StringItem s are simple read-only text elements that are initialized with the label and a text
String parameter only. The following code snippet shows the creation of a simple version label.
After creation, the label is added to the main form in the constructor of the HelloMidp application:
public HelloMidp() {
mainForm = new Form ("HelloMidp");
StringItem versionItem = new StringItem ("Version: ", "1.0");
mainForm.append (versionItem);
}
The label of the StringItem can be accessed using the setLabel() and getLabel() methods
inherited from Item . To access the text, you can use the methods setText() and getText() .
ImageItem
Similar to the StringItem , the ImageItem is a plain non-interactive Item . In addition to the label,
the ImageItem constructor takes an Image object, a layout parameter, and an alternative text string
that is displayed when the device is not able to display the image. The image given to the constructor
must be non-mutable. All images loaded from the MIDlet suite's JAR file are not mutable.
The difference between mutable and non-mutable Image s is described in more detail in the section
about Image s in this chapter. For now, we will treat the Image class as a "black box" that has a string
constructor that denotes the location of the image in the JAR file. Please note that Image construction
from a JAR file throws an IOException if the image cannot be loaded for some reason. The layout
parameter is one of the integer constants listed in Table 3.2 , where the newline constants can be
combined with the horizontal alignment constants.
 
Search WWH ::




Custom Search