Java Reference
In-Depth Information
3. Enter the code as shown in Figure 5-18 on the previous page, replacing
the programmer name and date shown with your name and the current
date.
The TextPad window displays the beginning code for the Reservations class
(Figure 5-19).
block
comment
import
statements
class header
FIGURE 5-19
Using a Choice Component
A Choice component is an AWT component just like Buttons, Labels, and
TextFields; however, the Choice component displays as a drop-down list box
with a box arrow. Choice components are useful for displaying restricted lists,
such as specific states or countries, a list of available colors, or numbers within a
range. Java automatically displays the first item in the list and then displays a box
arrow that a user can click to view other items in the Choice component list.
Users then can select any item in the list.
When a user clicks the box arrow and selects an item from the list, Java asso-
ciates that click event with a Choice event. The Choice event of clicking the
arrow to display the list and then choosing from that list is polymorphic — that
is, it happens without any additional components or coding. When you add the
Choice component, the functionality comes with it. As you will see later in this
chapter, Choice components have some unique features with regard to their cre-
ation and population, but they must be constructed with a name and then added
to the user interface in a manner similar to other components.
Using an = new constructor, the Choice component must be given a name.
The following code:
Choice myChoice = new Choice();
identifies Choice as the type of component and myChoice as the name of this
specific instance of the Choice component. The object type, Choice, must be
capitalized. As with the CheckboxGroup component you learned about in
an earlier chapter, the Choice component's argument list is null — that is, no
arguments are listed inside the parentheses during construction.
 
Search WWH ::




Custom Search