Java Reference
In-Depth Information
You can also specify a list of strings and images, one for each choice the item
should show.
The
Choice
interface defines two properties to manage what you have selected
from the interface. For exclusive and pop-up lists, the selected index property (with its
accessor
getSelectedIndex
and mutator
setSelectedIndex
) is probably the most useful,
as it returns the index into the list of choices of the currently selected item. For multiple-
selection lists, you want to use
setSelectedFlags
and
getSelectedFlags
, which give you
the status of each selected and unselected item through an array of
boolean
flags you
provide to the method. You can also invoke
isSelected
with an index to determine if the
user has selected a particular item, and
setSelectedIndex
to set the selection
status of a particular item.
Because
Choice
manages a collection of user choices, it has a collection-oriented inter-
face with the following methods that let you access and mutate the list of user choices:
•
append
: Lets you append a new user choice (string and image) to the list of choices
being presented
•
delete
: Takes an index and deletes the user choice item at the index you specified
•
deleteAll
: Deletes all user choices, resulting in an empty collection of choices
•
insert
: Inserts a new choice item (string and index) after the indicated index
•
size
: Returns the number of user choices in the group
You can also mutate an item's appearance or contents using the
setFont
method to
change the font for a specific item.
Introducing the Screen and Its Subclasses
In the beginning of this chapter, I told you how the
Display
class uses
Displayable
subclasses to manage what to display. For high-level user-interface programming, the
Screen
subclasses
Form
,
Alert
,
TextBox
, and
List
are the only game in town. These let you
put together complex user interfaces quickly, albeit sacrificing some of the control (such
as per-pixel placement) of the
Canvas
class.
Collecting Visible Items Using the Form Class
The most flexible subclass of
Screen
is the
Form
class. As I've already said, it acts as a
collection and layout class for
Item
instances, letting you combine various
Item
subclass
instances to create screens with a variety of user-interface controls.