Multiple-Selection Lists
A multiple-selection list enables the user to select many items from a JList (see output of Fig. 11.26). A SINGLE_INTERVAL_SELECTION list allows selecting a contiguous range of items. To do so, click the first item, then press and hold the Shift key while clicking the last item in the range. A MULTIPLE_INTERVAL_SELECTION list allows continuous range selection as described for a SINGLE_INTERVAL_SELECTION list. Such a list allows miscellaneous items to be selected by pressing and holding the Ctrl key (sometimes called the Control key) while clicking each item to select. To deselect an item, press and hold the Ctrl key while clicking the item a second time.
The application of Fig. 11.25 and Fig. 11.26 uses multiple-selection lists to copy items from one JList to another. One list is a MULTIPLE_INTERVAL_SELECTION list and the other is a SINGLE_INTERVAL_SELECTION list. When you execute the application, try using the selection techniques described previously to select items in both lists.
Figure 11.25. JList that allows multiple selections.
(This item is displayed on pages 549 - 550 in the print version)
1 // Fig. 11.25: MultipleSelectionFrame.java 2 // Copying items from one List to another. 3 import java.awt.FlowLayout; 4 import java.awt.event.ActionListener; 5 import java.awt.event.ActionEvent; 6 import javax.swing.JFrame; 7 import javax.swing.JList; 8 import javax.swing.JButton; 9 import javax.swing.JScrollPane; 10 import javax.swing.ListSelectionModel; 11 12 public class MultipleSelectionFrame extends JFrame 13 { 14 private JList colorJList; // list to hold color names 15 private JList copyJList; // list to copy color names into 16 private JButton copyJButton; // button to copy selected names 17 private final String colorNames[] = { "Black", "Blue", "Cyan", 18 "Dark Gray", "Gray", "Green", "Light Gray", "Magenta", "Orange", 19 "Pink", "Red", "White", "Yellow" }; 20 21 // MultipleSelectionFrame constructor 22 public MultipleSelectionFrame() 23 { 24 super( "Multiple Selection Lists" ); 25 setLayout( new FlowLayout() ); // set frame layout 26 27 colorJList = new JList( colorNames ); // holds names of all colors 28 colorJList.setVisibleRowCount( 5 ); // show five rows 29 colorJList.setSelectionMode( 30 ListSelectionModel.MULTIPLE_INTERVAL_SELECTION ); 31 add( new JScrollPane( colorJList ) ); // add list with scrollpane 32 33 copyJButton = new JButton( "Copy >>>" ); // create copy button 34 copyJButton.addActionListener( 35 36 new ActionListener() // anonymous inner class 37 { 38 // handle button event 39 public void actionPerformed( ActionEvent event ) 40 { 41 // place selected values in copyJList 42 copyJList.setListData( colorJList.getSelectedValues() ); 43 } // end method actionPerformed 44 } // end anonymous inner class 45 ); // end call to addActionListener 46 47 add( copyJButton ); // add copy button to JFrame 48 49 copyJList = new JList(); // create list to hold copied color names 50 copyJList.setVisibleRowCount( 5 ); // show 5 rows 51 copyJList.setFixedCellWidth( 100 ); // set width 52 copyJList.setFixedCellHeight( 15 ); // set height 53 copyJList.setSelectionMode( 54 ListSelectionModel.SINGLE_INTERVAL_SELECTION ); 55 add( new JScrollPane( copyJList ) ); // add list with scrollpane 56 } // end MultipleSelectionFrame constructor 57 } // end class MultipleSelectionFrame |
Figure 11.26. Test class for MultipleSelectionFrame.
(This item is displayed on pages 550 - 551 in the print version)
1 // Fig. 11.26: MultipleSelectionTest.java 2 // Testing MultipleSelectionFrame. 3 import javax.swing.JFrame; 4 5 public class MultipleSelectionTest 6 { 7 public static void main( String args[] ) 8 { 9 MultipleSelectionFrame multipleSelectionFrame = 10 new MultipleSelectionFrame(); 11 multipleSelectionFrame.setDefaultCloseOperation( 12 JFrame.EXIT_ON_CLOSE ); 13 multipleSelectionFrame.setSize( 350, 140 ); // set frame size 14 multipleSelectionFrame.setVisible( true ); // display frame 15 } // end main 16 } // end class MultipleSelectionTest
|
Line 27 of Fig. 11.25 creates JList colorList and initializes it with the strings in the array colorNames. Line 28 sets the number of visible rows in colorList to 5. Lines 2930 specify that colorList is a MULTIPLE_INTERVAL_SELECTION list. Line 31 adds a new JScrollPane containing colorList to the JFrame. Lines 4955 perform similar tasks for copyList, which is declared as a SINGLE_INTERVAL_SELECTION list. Line 51 uses JList method setFixedCellWidth to set copyList's width to 100 pixels. Line 52 uses JList method setFixedCellHeight to set the height of each item in the JList to 15 pixels.
There are no events to indicate that a user has made multiple selections in a multiple-selection list. Normally, an event generated by another GUI component (known as an external event) specifies when the multiple selections in a JList should be processed. In this example, the user clicks the JButton called copyButton to trigger the event that copies the selected items in colorList to copyList.
Lines 3945 declare, create and register an ActionListener for the copyButton. When the user clicks copyButton, method actionPerformed (lines 3943) uses JList method setListData to set the items displayed in copyList. Line 42 calls colorList's method getSelectedValues, which returns an array of Objects representing the selected items in colorList. In this example, the returned array is passed as the argument to copyList's setListData method.
You might be wondering why copyList can be used in line 42 even though the application does not create the object to which it refers until Line 49? Remember that method actionPerformed (lines 3943) does not execute until the user presses the copyButton, which cannot occur until after the constructor completes execution and the application displays the GUI. At that point in the application's execution, copyList is already initialized with a new JList object.