A combo box (sometimes called a drop-down list) enables the user to select one item from a list (Fig. I.9). Combo boxes are implemented with class JComboBox, which extends class JComponent
. 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 handling. The app (Figs. I.8–I.9) uses a JComboBox
to provide a list of four imagefile names from which the user can select one image to display. When the user selects a name, the app displays the corresponding image as an Icon
on a JLabel
. Class ComboBoxTest
(Fig. I.9) contains the main
method that executes this app. The screen captures for this app show the JComboBox
list after the selection was made to illustrate which image-file name was selected.
Lines 19–23 (Fig. I.8) declare and initialize array icons
with four new ImageIcon
objects. String
array names
(lines 17–18) contains the names of the four image files that are stored in the same directory as the app.
1 // Fig. I.8: ComboBoxFrame.java
2 // JComboBox that displays a list of image names.
3 import java.awt.FlowLayout;
4 import java.awt.event.ItemListener;
5 import java.awt.event.ItemEvent;
6 import javax.swing.JFrame;
7 import javax.swing.JLabel;
8 import javax.swing.JComboBox;
9 import javax.swing.Icon;
10 import javax.swing.ImageIcon;
11
12 public class ComboBoxFrame extends JFrame
13 {
14 private JComboBox imagesJComboBox; // combobox to hold names of icons
15 private JLabel label; // label to display selected icon
16
17 private static final String[] names =
18 { "bug1.gif", "bug2.gif", "travelbug.gif", "buganim.gif" };
19 private Icon[] icons = {
20 new ImageIcon( getClass().getResource( names[ 0 ] ) ),
21 new ImageIcon( getClass().getResource( names[ 1 ] ) ),
22 new ImageIcon( getClass().getResource( names[ 2 ] ) ),
23 new ImageIcon( getClass().getResource( names[ 3 ] ) ) };
24
25 // ComboBoxFrame constructor adds JComboBox to JFrame
26 public ComboBoxFrame()
27 {
28 super( "Testing JComboBox" );
29 setLayout( new FlowLayout() ); // set frame layout
30
31 imagesJComboBox = new JComboBox( names ); // set up JComboBox
32 imagesJComboBox.setMaximumRowCount( 3 ); // display three rows
33
34 imagesJComboBox.addItemListener(
35 new ItemListener() // anonymous inner class
36 {
37 // handle JComboBox event
38 public void itemStateChanged( ItemEvent event )
39 {
40 // determine whether item selected
41 if ( event.getStateChange() == ItemEvent.SELECTED )
42 label.setIcon( icons[
43 imagesJComboBox.getSelectedIndex() ] );
44 } // end method itemStateChanged
45 } // end anonymous inner class
46 ); // end call to addItemListener
47
48 add( imagesJComboBox ); // add combobox to JFrame
49 label = new JLabel( icons[ 0 ] ); // display first icon
50 add( label ); // add label to JFrame
51 } // end ComboBoxFrame constructor
52 } // end class ComboBoxFrame
1 // Fig. I.9: 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 ); // set frame size
12 comboBoxFrame.setVisible( true ); // display frame
13 } // end main
14 } // end class ComboBoxTest
At line 31, the constructor initializes a JComboBox
object with the String
s in array names
as the elements in the list. Each item in the list has an index. The first item is added at index 0, the next at index 1 and so forth. The first item added to a JComboBox
appears as the currently selected item when the JComboBox
is displayed. Other items are selected by clicking the JComboBox
, then selecting an item from the list that appears.
Line 32 uses JComboBox
method setMaximumRowCount to set the maximum number of elements that are displayed when the user clicks the JComboBox
. If there are additional items, the JComboBox
provides a scrollbar (see the first screen) that allows the user to scroll through all the elements in the list. The user can click the scroll arrows at the top and bottom of the scrollbar to move up and down through the list one element at a time, or else drag the scroll box in the middle of the scrollbar up and down. To drag the scroll box, position the mouse cursor on it, hold the mouse button down and move the mouse. In this example, the drop-down list is too short to drag the scroll box, so you can click the up and down arrows or use your mouse’s wheel to scroll through the four items in the list.
Line 48 attaches the JComboBox
to the ComboBoxFrame
’s FlowLayout
(set in line 29). Line 49 creates the JLabel
that displays ImageIcon
s and initializes it with the first ImageIcon
in array icons
. Line 50 attaches the JLabel
to the ComboBoxFrame
’s FlowLayout
.