A list is a component that basically performs the same function as a combo box but enables the user to choose a single value or multiple values. The Swing JList is very versatile. Figure 15.23 lists several frequently used constructors and methods in JList .
selectionMode is one of the three values ( SINGLE_SELECTION , SINGLE_INTERVAL_SELECTION , MULTIPLE_INTERVAL_SELECTION ) defined in javax.swing.SelectionModel that indicate whether a single item, single-interval item, or multiple-interval item can be selected. Single selection allows only one item to be selected. Single-interval selection allows multiple selections, but the selected items must be contiguous. Multiple-interval selection allows selections of multiple contiguous items without restrictions, as shown in Figure 15.24. The default value is MULTIPLE_INTERVAL_SELECTION .
The following statements create a list with six items, red foreground, white background, pink selection foreground, black selection background, and visible row count 4:
JList jlst = new JList( new Object[] { "Item 1" , "Item 2" , "Item 3" , "Item 4" , "Item 5" , "Item 6" }); jlst.setForeground(Color.red); jlst.setBackground(Color.white); jlst.setSelectionForeground(Color.pink); jlst.setSelectionBackground(Color.black); jlst.setVisibleRowCount( 4 );
Lists do not scroll automatically. To make a list scrollable, create a scroll pane and add the list to it. Text areas are made scrollable in the same way.
JList generates javax.swing.event.ListSelectionEvent to notify the listeners of the selections. The listener must implement the valueChanged handler in the javax.swing.event.ListSelectionListener interface to process the event.
Listing 15.8 gives a program that lets users select countries in a list and display the flags of the selected countries in the labels. Figure 15.25 shows a sample run of the program.
Here are the major steps in the program:
1. | Create the user interface. Create a list with nine country names as selection values, and place the list inside a scroll pane. Place the scroll pane in the west of the frame. Create nine labels to be used to display the countries' flag images. Place the labels in the panel, and place the panel in the center of the frame. |
| |
2. | Process the event. Create a listener to implement the valueChanged method in the ListSelectionListener interface to set the selected countries' flag images in the labels. |
1 import java.awt.*; 2 import javax.swing.*; 3 import javax.swing.event.*; 4 5 public class ListDemo extends JFrame { 6 final int NUMBER_OF_FLAGS = 9 ; 7 8 // Declare an array of Strings for flag titles 9 private String[] flagTitle s = { "Canada" , "China" , "Denmark" , 10 "France" , " Germany " , "India" , "Norway" , "United Kingdom" , 11 "United States of America" }; 12 13 // The list for selecting countries 14 private JList jlst = new JList(flagTitles); 15 16 // Declare an ImageIcon array for the national flags of 9 countries 17 private ImageIcon[] imageIcons = { 18 new ImageIcon( "image/ca.gif" ), 19 new ImageIcon( "image/china.gif" ), 20 new ImageIcon( "image/denmark.gif" ), 21 new ImageIcon( "image/fr.gif" ), 22 new ImageIcon( "image/germany.gif" ), 23 new ImageIcon( "image/india.gif" ), 24 new ImageIcon( "image/norway.gif" ), 25 new ImageIcon( "image/uk.gif" ), 26 new ImageIcon( "image/us.gif" ) 27 }; 28 29 // Arrays of labels for displaying images 30 private JLabel[] jlblImageViewer = new JLabel[NUMBER_OF_FLAGS]; 31 32 public static void main(String[] args) { 33 ListDemo frame = new ListDemo(); 34 frame.setSize( 650 , 500 ); 35 frame.setTitle( "ListDemo" ); 36 frame.setLocationRelativeTo( null ); // Center the frame 37 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 38 frame.setVisible( true ); 39 } 40 41 public ListDemo() { 42 // Create a panel to hold nine labels 43 JPanel p = new JPanel( new GridLayout( 3 , 3 , 5 , 5 )); 44 45 for ( int i = ; i < NUMBER_OF_FLAGS; i++) { 46 p.add(jlblImageViewer[i] = new JLabel()); 47 jlblImageViewer[i].setHorizontalAlignment 48 (SwingConstants.CENTER); 49 } 50 51 // Add p and the list to the frame 52 add(p, BorderLayout.CENTER); 53 add( new JScrollPane(jlst), BorderLayout.WEST); 54 55 // Register listeners 56 jlst.addListSelectionListener( new ListSelectionListener() { 57 /** Handle list selection */ 58 public void valueChanged(ListSelectionEvent e) { 59 // Get selected indices 60 int [] indices = jlst.getSelectedIndices(); 61 62 int i; 63 // Set icons in the labels 64 for (i = ; i < indices.length; i++) { 65 jlblImageViewer[i].setIcon(imageIcons[indices[i]]); 66 } 67 68 // Remove icons from the rest of the labels 69 for (; i < NUMBER_OF_FLAGS; i++) { 70 jlblImageViewer[i].setIcon( null ); 71 } 72 } 73 }); 74 } 75 } |
The anonymous inner class listener listens to ListSelectionEvent for handling the selection of country names in the list (lines 56 “73). ListSelectionEvent and ListSelectionListener are defined in the javax.swing.event package, so this package is imported in the program (line 3).
The program creates an array of nine labels for displaying flag images for nine countries. The program loads the images of the nine countries into an image array (lines 17 “27) and creates a list of the nine countries in the same order as in the image array (lines 9 “11). Thus the index of the image array corresponds to the first country in the list.
The list is placed in a scroll pane (line 53) so that it can be scrolled when the number of items in the list extends beyond the viewing area.
By default, the selection mode of the list is multiple-interval, which allows the user to select multiple items from different blocks in the list. When the user selects countries in the list, the valueChanged handler (lines 58 “72) is executed, which gets the indices of the selected item and sets their corresponding image icons in the label to display the flags.