Java Reference
In-Depth Information
// Allow only one of the items to be selected.
color
-
list.setSelectionMode (
ListSelectionModel.SINGLE
-
SELECTION);
// Select initially the top item.
color
-
list.setSelectedIndex (0);
color
-
list.addListSelectionListener (this);
// Add to a JScrollPane so that we can have
// a scroller to view other items.
JScrollPane scroll
-
pane
=
new JScrollPane (color
-
list);
add (scroll
-
pane);
}
// ctor
// This class implements the ListSelectionListener
// so events come to this valueChanged method.
public void valueChanged (ListSelectionEvent evt)
{
// Default color component values.
int cr=0;
int cg=0;
int cb=0;
// Get the reference to the JList object
JList source = (JList)evt.getSource ();
// and get an array of the selected items.
Object [] values
=
source.getSelectedValues ();
// In this case only one value can be selected
// so just look at first item in array.
String color
-
selected
=
(String)values[0];
if (color
-
selected.equals (
"
Red
"
))
cr
=
0xFF;
else if (color
-
selected.equals (
"
Green
"
))
cg
=
0xFF;
else if (color
-
selected.equals (
"
Blue
"
))
cb
=
0xFF;
else if (color
-
selected.equals (
"
Yellow
"
))
{
cr
=
0xFF;
cg
=
0xFF;
}
else if (color
-
selected.equals ("White")) {
cr = 0xFF;
Search WWH ::
Custom Search