Java Reference
In-Depth Information
49
add(imagesJComboBox);
// add combo box to JFrame
50
label =
new
JLabel(icons[
0
]);
// display first icon
51
add(label);
// add label to JFrame
52
}
53
}
// end class ComboBoxFrame
Fig. 12.21
|
JComboBox
that displays a list of image names. (Part 2 of 2.)
1
// Fig. 12.22: ComboBoxTest.java
2
// Testing ComboBoxFrame.
3
import
javax.swing.JFrame;
4
5
public
class
ComboBoxTest
6
{
7
public static void
main(String[] args)
8
{
9
ComboBoxFrame comboBoxFrame =
new
ComboBoxFrame();
10
comboBoxFrame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE
);
11
comboBoxFrame.setSize(
350
,
150
);
12
comboBoxFrame.setVisible(
true
);
13
}
14
}
// end class ComboBoxTest
Scroll box
Scrollbar to scroll through the
items in the list
Scroll arrows
Fig. 12.22
|
Testing
ComboBoxFrame
.
JComboBox
es generate
ItemEvent
s just as
JCheckBox
es and
JRadioButton
s do. This
example also demonstrates a special form of inner class that's used frequently in event han-
dling. The application (Figs. 12.21-12.22) uses a
JComboBox
to provide a list of four image
filenames from which the user can select one image to display. When the user selects a
name, the application displays the corresponding image as an
Icon
on a
JLabel
. Class
Com-
boBoxTest
(Fig. 12.22) contains the
main
method that executes this application. The
screen captures for this application show the
JComboBox
list after the selection was made
to illustrate which image filename was selected.