Java Reference
In-Depth Information
group.add (fBlue);
add (fBlue);
} // ctor
/** Action events come here when the user clicks
*onabutton.
**/
public void actionPerformed (ActionEvent evt) {
// Default color component values.
int cr=0;
int cg=0;
int cb=0;
// Only one button can be selected at a time
// so find the button and set the corresponding
// output panel color.
Object source
=
evt.getSource ();
if (source
==
fRed)
cr
=
0xFF;
else if (source
==
fGreen)
cg
=
0xFF;
else if (source
==
fBlue)
cb
=
0xFF;
fOutputPanel.setColor (cr,cg,cb);
} // actionPerformed
} // class RadioButtonsPanel
7.3.3
JList
The
JList
component provides a list of items from which the user can select.
Depending on how the
JList
is configured, either a single item or multiple items
can be selected. This time we tell
UiTestApplet
to use
ListApplet
,which
creates the interface shown in Figure 7.6. The user can select one of five colors
in a
JList
. The
ListPanel
code is given below.
One constructor of
JList
takes an
Object
array as a parameter. The ele-
ments of the array become the list items. We'll use a
String
array. The
number of items that will be visible depends on the value passed to the
setVisibleRowCount()
method. The
setSelectionModel()
is used to
configure the list to be either single- or multiple-selection mode, and the initial
item selected is set with
setSelectedIndex()
.
Search WWH ::
Custom Search