The basic features of JList were introduced in §15.9, "Lists," without using list models. You learned how to create a list and how to respond to list selections. However, you cannot add or remove elements from a list without using list models. This section introduces list models and gives a detailed discussion on how to use JList .
JList has two supporting models: a list model and a list-selection model. The list model is for storing and processing data. The list-selection model is for selecting items. By default, items are rendered as strings or icons. You can also create a custom renderer implementing the ListCellRenderer interface. The relationship of these interfaces and classes is shown in Figure 30.15.
Figure 30.16 shows the properties and constructors of JList . You can create a list from a list model, an array of objects, or a vector.
The layoutOrientation property, introduced in JDK 1.4, specifies the layout of the items using one of the following three values:
JList.VERTICAL specifies that the cells should be laid out vertically in one column. This is the default value.
JList.HORIZONTAL_WRAP specifies that the cells should be laid out horizontally, wrapping to a new row as necessary. The number of rows to use is determined by the visibleRowCount property if its value is greater than ; otherwise the number of rows is determined by the width of the JList .
JList.VERTICAL_WRAP specifies that the cells should be laid out vertically, wrapping to a new column as necessary. The number of rows to use is determined by the visibleRowCount property if its value is greater than ; otherwise the number of rows is determined by the height of the JList .
For example, suppose there are five elements (item1, item2, item3, item4, and item5) in the list and the visibleRowCount is 2 . Figure 30.17 shows the layout in these three cases.
The selectionMode property is one of the three values ( SINGLE_SELECTION , SINGLE_INTERVAL_SELECTION , MULTIPLE_INTERVAL_SELECTION ) that indicate whether a single item, single-interval item, or multiple-interval item can be selected, as shown in Figure 30.18. Single selection allows only one item to be selected. Single-interval selection allows multiple selections, but the selected items must be contiguous. These items can be selected all together by holding down the SHIFT key. Multiple-interval selection allows selections of multiple contiguous items without restrictions. These items can be selected by holding down the CTRL key. The default value is MULTIPLE_INTERVAL_SELECTION .
The selectionModel property specifies an object that tracks list selection. JList has two models: a list model and a list-selection model. List models handle data management, and list-selection models deal with data selection. A list-selection model must implement the ListSelectionModel interface, which defines constants for three selection modes ( SINGLE_SELECTION , SINGLE_INTERVAL_SELECTION , and MULTIPLE_INTERVAL_SELECTION ), and registration methods for ListSectionListener . It also defines the methods for adding and removing selection intervals, and the access methods for the properties, such as selectionMode , anchorSelectionIndex , leadSelectionIndex , and valueIsAdjusting .
By default, an instance of JList uses DefaultListSelectionModel , which is a concrete implementation of ListSelectionModel . Usually, you do not need to provide a custom list-selection model, because the DefaultListSelectionModel class is sufficient in most cases. List-selection models are rarely used explicitly, because you can set the selection mode directly in JList .
This example creates a list of a fixed number of items displayed as strings. The example enables you to dynamically set visibleRowCount from a spinner, layoutOrientation from a combo box, and selectionMode from a combo box, as shown in Figure 30.19. When you select one or more items, their values are displayed in a status label below the list. The source code of the example is given in Listing 30.7.
1 import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4 import javax.swing.event.*; 5 6 public class ListPropertiesDemo extends JApplet { 7 private JList jlst = new JList( new String[] { "Item1" , 8 "Item2" , "Item3" , "Item4" , "Item5" , "Item6" } ); 9 private JSpinner jspinnerVisibleRowCount = 10 new JSpinner( new SpinnerNumberModel( 8 , - 1 , 20 , 1 )); 11 private JComboBox jcboLayoutOrientation = 12 new JComboBox( new String[]{ "VERTICAL (0)" , 13 "VERTICAL_WRAP (1)" , "HORIZONTAL_WRAP (2)" }); 14 private JComboBox jcboSelectionMode = 15 new JComboBox( new String[]{ "SINGLE_SELECTION (0)" , 16 "SINGLE_INTERVAL_SELECTION (1)" , 17 "MULTIPLE_INTERVAL_SELECTION (2)" }); 18 private JLabel jlblStatus = new JLabel(); 19 20 /** Construct the applet */ 21 public ListPropertiesDemo() { 22 // Place labels in a panel 23 JPanel panel1 = new JPanel(); 24 panel1.setLayout( new GridLayout( 3 , 1 )); 25 panel1.add( new JLabel( "visibleRowCount" )); 26 panel1.add( new JLabel( "layoutOrientation" )); 27 panel1.add( new JLabel( "selectionMode" )); 28 29 // Place text fields in a panel 30 JPanel panel2 = new JPanel(); 31 panel2.setLayout( new GridLayout( 3 , 1 )); 32 panel2.add(jspinnerVisibleRowCount); 33 panel2.add(jcboLayoutOrientation); 34 panel2.add(jcboSelectionMode); 35 36 // Place panel1 and panel2 37 JPanel panel3 = new JPanel(); 38 panel3.setLayout( new BorderLayout( 5 , 5 )); 39 panel3.add(panel1, BorderLayout.WEST); 40 panel3.add(panel2, BorderLayout.CENTER); 41 42 // Place elements in the applet 43 add(panel3, BorderLayout.NORTH); 44 add( new JScrollPane(jlst) , BorderLayout.CENTER); 45 add(jlblStatus, BorderLayout.SOUTH); 46 47 // Set initial property values 48 jlst.setFixedCellWidth( 50 ); 49 jlst.setFixedCellHeight( 20 ); 50 jlst.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 51 52 // Register listeners 53 jspinnerVisibleRowCount.addChangeListener( new ChangeListener() { 54 public void stateChanged(ChangeEvent e) { 55 jlst.setVisibleRowCount( 56 ((Integer)jspinnerVisibleRowCount.getValue()).intValue()) ; 57 } 58 }); 59 60 jcboLayoutOrientation.addActionListener( new ActionListener() { 61 public void actionPerformed(ActionEvent e) { 62 jlst.setLayoutOrientation( 63 jcboLayoutOrientation.getSelectedIndex()) ; 64 } 65 }); 66 67 jcboSelectionMode.addActionListener( new ActionListener() { 68 public void actionPerformed(ActionEvent e) { 69 jlst.setSelectionMode( 70 jcboSelectionMode.getSelectedIndex()) ; 71 } 72 }); 73 74 jlst.addListSelectionListener( new ListSelectionListener() { 75 public void valueChanged(ListSelectionEvent e) { 76 Object[] values = jlst.getSelectedValues(); 77 String display = " " ; 78 79 for ( int i = ; i < values.length; i++) { 80 display += (String)values[i] + " " ; 81 } 82 83 jlblStatus.setText(display); 84 } 85 }); 86 } 87 } |
A JList is created with six string values (lines 7 “8). A JSpinner is created using a SpinnerNumberModel with initial value 8 , minimum value -1 , maximum value 20 , and step 1 (lines 9 “10). A JComboBox is created with string values VERTICAL (0) , VERTICAL_WRAP (1) , and HORIZONTAL_WRAP (2) for choosing layout orientation (lines 11 “13). A JComboBox is created with string values SINGLE_SELECTION (0) , INTERVAL_SELECTION (1) , and MULTIPLE_INTERVAL_SELECTION (2) for choosing a selection mode (lines 14 “17). A JLabel is created to display the selected elements in the list (lines 18).
A JList does not support scrolling. To create a scrollable list, create a JScrollPane and add an instance of JList to it (line 44).
The fixed list cell width and height are specified in lines 48 “49. The default selection mode is multiple-interval selection. Line 50 sets the selection mode to single selection.
When a new visible row count is selected from the spinner, the setVisibleRowCount method is used to set the count (lines 53 “58). When a new layout orientation is selected from the jcboLayoutOrientation combo box, the setLayoutOrientation method is used to set the layout orientation (lines 60 “65). Note that the constant values for VERTICAL , VERTICAL_WRAP , and HORIZONTAL_WRAP are 0, 1 , and 2 , which correspond to the index values of these items in the combo box. When a new selection mode is selected from the jcboSelectionMode combo box, the setSelectionMode method is used to set the selection mode (lines 67 “72). Note that the constant values for SINGLE_SELECTION , SINGLE_INTERVAL_SELECTION , and MULTIPLE_INTERVAL_SELECTION are , 1 , and 2 , which correspond to the index values of these items in the combo box.
JList generates javax.swing.event.ListSelectionEvent to notify the listeners of the selections. The listener must implement the valueChanged handler to process the event. When the user selects an item in the list, the valueChanged handler is executed, which gets the selected items and displays all the items in the label (lines 74 “85).