This section demonstrates how to create various GUI components. The MIDlet in the example shown here is used to test lists, forms, choices, gauges, text fields, and text boxes.
Lists
The method
startApp( )
starts the MIDlet by creating a list of items and associating the exit command with it.
The following list gives a line-by-line analysis of the program
GuiTests.java
shown in Figure 11.7.
The class
javax.microedition.lcdui.List
is a
Screen
containing a list of choices.
The constructor
public javax.microedition.lcdui.List.List(String title, int listType )
creates a new, empty
List
with the following two parameters:
The method
public int javax.microedition.lcdui.List.append( String stringPart, Image imagePart )
appends an element to the
Choice
, which is an interface of the package
javax.microedition.lcdui
and defines an API for a user interface components implementing a selection from a predefined number of choices. The added element will be the last element of the
Choice
.
The class
javax.microedition.lcdui.Ticker
implements a "ticker-tape," a piece of text that runs continuously across the display. The direction and speed of scrolling are determined by the implementation. While animated, the ticker string scrolls continuously. The constructor
public Ticker( String str )
constructs a new
Ticker
object, given its initial contents string.
The method
public void javax.microedition.lcdui.Screen.setTicker ( Ticker ticker )
sets a ticker for use with this
Screen
, replacing any previous ticker.
The class
javax.microedition.lcdui.Gauge
implements a bar graph display of a value intended for use in a form. The constructor
public Gauge( String label, boolean interactive, int maxValue, int initialValue )
creates a new
Gauge
object with the following parameters:
-
label
: the
Gauge
's label,
-
interactive
: interactive or non-interactive mode. In interactive mode, the user is allowed to modify the value.
-
maxValue
: the maximum value must be greater than zero.
-
initialValue
: the initial value must be within the range zero to
maxValue
, inclusive.
For example, consider a
Gauge
object that has a range of values from zero to 99, running on a device that displays the
Gauge
's approximate value using a set of one to ten bars. The device might show one bar for values zero through nine, two bars for values ten through 19, three bars for values 20 through 29, and so forth.
The class
javax.microedition.lcdui.TextField
is an editable text component that may be placed into a
Form
. The constructor
public TextField( String label, String text, int maxSize, int constraints )
creates a new
TextField
object with the following parameters:
-
label
: the item label.
-
text
: the initial contents, or null if the
TextField
is to be empty.
-
maxSize
: the maximum capacity in characters.
-
constraints
: constant 0 allows users to enter any text. Other values can be found from the
Field Summary
of input constraints.
The class
javax.microedition.lcdui.Form
is a
Screen
that contains an arbitrary mixture of items: images, read-only text fields, editable text fields, editable date fields, gauges, and choice groups. The constructor
public Form( String title )
creates a new, empty
Form
.
-
form.append(gauge)
;
form.append(textfield)
;
The method
public int javax.microedition.lcdui.Form.append(Item item)
adds an
Item
into the
Form
. An
Item
is a superclass for components that can be added to a
and
Alert
.
Form
The class
javax.microedition.lcdui.DateField
is an editable component for presenting date and time (calendar) information that may be placed into a
Form
. The constructor
public DateField(String label, int mode)
creates a
DateField
object with the following parameters:
The method
public int getSelectedIndex( )
returns the index number of an element in
Choice
that has been selected. For
Choice
types
EXCLUSIVE
and
IMPLICIT
there is at most one element selected, so this method is useful for determining the user's selection.
Other Interfaces
Figure 11.10 shows screenshots of other interfaces from the project
GuiSuite
.
The class
javax.microedition.lcdui.Alert
is a screen that shows data to the user and waits for a certain period of time before proceeding to the next screen. An alert is an ordinary screen that can contain text and image, and which handles events like other screens. The constructor
public Alert( String title )
constructs a new, empty
object with the given title.
Alert
The method
public void setType( AlertType type )
sets the type of the
Alert
. The handling and behavior of specific
AlertTypes
is described in
AlertType
. An
is designed to alert the user to an erroneous operation.
ERROR AlertType
The method
public void setString( String str )
sets the text string used in the
Alert
.
The class
java.util.Date
represents a specific instant in time, with millisecond precision. The constructor
public Date( )
allocates a
Date
object and initializes it to represent the current time specified number of milliseconds since the standard base time known as "the epoch," namely January 1, 1970, 00:00:00 GMT.
The method
public void javax.microedition.lcdui.DateField.setDate( Date date )
sets a new value for this field.