Java Reference
In-Depth Information
mainForm = new Form("String Item Demo", new Item[] {
get_stringItem1(),
get_stringItem2(),
});
Passing a list of
Item
instances to the constructor inserts the instances in the same
order on the display, and is thus a common way to construct whole screens at a time.
The
Form
class really is a collection, though, and offers several methods to manipulate
the items it displays. I show you those in a later section in this chapter, “Collecting Visible
Items Using the Form Class.” For now, though, you should know that you can also add a
single item to the
Form
instance using its
append
and
insert
methods. The
append
method
adds the
Item
instance to the end of the list of items on the
Form
, while the
insert
method
inserts the item between two other items on the
Form
.
Introducing Items
For your user interface, the rubber hits the road with the
Item
class and its subclass.
Unless you're coding raw interface code using the
Canvas
or
GameCanvas
classes (which I
discuss later), each visible item in your user interface must implement the
Item
interface
or else be a wholly separate
Displayable
object such as an
Alert
,
TextBox
, or
List
. The
Item
class encapsulates the following responsibilities:
•
Command management
: An
Item
can send commands to the
Form
's command
listener in response to user keystrokes such as selections or other actions. In addi-
tion, an individual
Item
can have its own command listeners.
•
Drawing
: An
Item
knows how to draw itself.
•
Event handling
: An
Item
knows how to handle raw events such as keystrokes, so
your application doesn't have to.
•
Layout preferences
: An
Item
signals to its containing form its preferences about how
it should be laid out, and then the
Form
uses its layout policy to present all of the
items on the display in a cohesive way.
The
Item
class provides three methods for managing commands:
•
addCommand
: Lets you add a
Command
instance of type
ITEM
to the item. In turn,
the MIDP implementation presents this command when the item is active
(highlighted).
•
removeCommand
: Removes the indicated command from the item.
•
setItemCommandListener
: Sets a listener for
ITEM
commands on this item.