Introduction to Java Programming-Comprehensive Version (6th Edition)

 

[Page 1035 ( continued )]

30.10. JComboBox

The basic features of JComboBox were introduced in §15.8, "Combo Boxes," without using combo box models. This section introduces combo models and discusses the use of JComboBox in some detail.

A combo box is similar to a list. Combo boxes and lists are both used for selecting items from a list. A combo box allows the user to select one item at a time, whereas a list permits multiple selections. A combo box displays a drop-down list contained in a popup menu when the combo box is selected. The selected item can be edited in the cell as if it were a text field. Figure 30.25 shows the properties and constructors of JComboBox . The data for a combo box is stored in ComboBoxModel . You can create a combo box from a combo box model, an array of objects, or a vector.


[Page 1036]
Figure 30.25. JComboBox displays elements in a list.

JComboBox delegates the responsibilities of storing and maintaining data to its data model. All combo box models implement the ComboBoxModel interface, which extends the ListModel interface and defines the getSelectedItem and setSelectedItem methods for retrieving and setting a selected item. The methods for adding and removing items are defined in the MutableComboBoxModel interface, which extends ComboBoxModel . When an instance of JComboBox is created without explicitly specifying a model, an instance of DefaultComboBoxModel is used. The DefaultComboBoxModel class extends AbstractListModel and implements MutableComboBoxModel , as shown in Figure 30.26.

Figure 30.26. ComboBoxModel stores and manages data in a combo box.

(This item is displayed on page 1037 in the print version)

Usually you don't need to use combo box models explicitly, because JComboBox contains the methods for retrieving ( getItemAt , getSelectedItem , and getSelectedIndex ), adding ( addItem and insertItemAt ), and removing ( removeItem , removeItemAt , and removeAllItems ) items from the list.

JComboBox can generate ActionEvent and ItemEvent , among many other events. Whenever a new item is selected, JComboBox generates ItemEvent twice, once for deselecting the previously selected item, and the other for selecting the currently selected item. JComboBox generates an ActionEvent after generating an ItemEvent .

Combo boxes render cells exactly like lists, because the combo box items are displayed in a list contained in a popup menu. Therefore, a combo box cell renderer can be created exactly like a list cell renderer by implementing the ListCellRenderer interface. Like JList , JComboBox has a default cell renderer that displays a string or an icon, but not both at the same time. To display a combination of a string and an icon, you need to create a custom renderer. The custom list cell renderer MyListCellRenderer in Listing 30.9 can be used as a combo box cell renderer without any modification.


[Page 1037]

Listing 30.11 gives an example that creates a combo box to display the flag image and name for each country as one item in the list, as shown in Figure 30.27. When a country is selected in the list, its flag is displayed in a panel below the combo box.

Figure 30.27. The image and the text are displayed in the list cell of a combo box.

Listing 30.11. ComboBoxCellRendererDemo.java

(This item is displayed on pages 1037 - 1038 in the print version)

1 import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4 5 public class ComboBoxCellRendererDemo extends JApplet { 6 private final static int NUMBER_OF_NATIONS = 7 ; 7 private String[] nations = new String[] { "Denmark" , 8 "Germany" , "China" , "India" , "Norway" , "UK" , "US" };


[Page 1038]

9 private ImageIcon[] icons = new ImageIcon[NUMBER_OF_NATIONS]; 10 private ImageIcon[] bigIcons = new ImageIcon[NUMBER_OF_NATIONS]; 11 12 // Create a combo box model 13 private DefaultComboBoxModel model = new DefaultComboBoxModel(); 14 15 // Create a combo box with the specified model 16 private JComboBox jcboCountries = new JComboBox(model); 17 18 // Create a list cell renderer 19 private MyListCellRenderer renderer = new MyListCellRenderer(); 20 21 // Create a label for displaying image 22 private JLabel jlblImage = new JLabel( "" , JLabel.CENTER); 23 24 /** Construct the applet */ 25 public ComboBoxCellRendererDemo() { 26 // Load small and large image icons 27 for ( int i = ; i < NUMBER_OF_NATIONS; i++) { 28 icons[i] = new ImageIcon(getClass().getResource( 29 "image/flagIcon" + i + ".gif" )); 30 model.addElement( new Object[]{icons[i], nations[i]}); 31 32 bigIcons[i] = new ImageIcon(getClass().getResource( 33 "image/flag" + i + ".gif" )); 34 } 35 36 // Set list cell renderer for the combo box 37 jcboCountries.setRenderer(renderer); 38 jlblImage.setIcon(bigIcons[ ]); 39 add(jcboCountries, java.awt.BorderLayout.NORTH); 40 add(jlblImage, java.awt.BorderLayout.CENTER); 41 42 // Register listener 43 jcboCountries.addActionListener( new ActionListener() { 44 public void actionPerformed(java.awt.event.ActionEvent e) { 45 jlblImage.setIcon(bigIcons[jcboCountries.getSelectedIndex()]); 46 } 47 }); 48 } 49 }

The program is very similar to the preceding example in Listing 30.10. Two types of image icons are loaded for each country and stored in the arrays icons and bigIcons (lines 27 “34). Each item in the combo box is an array that consists of an icon and a string (line 30).

MyListCellRenderer , defined in the preceding example, is used to create a cell renderer in line 19. The cell renderer is plugged into the combo box in line 37.

When you choose a country from the combo box, the action event handler is invoked (lines 44 “46). This handler sets a new image on the label (line 45).

 

Категории