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:
 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;
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" };
21 // MultipleSelectionFrame constructor
22 public MultipleSelectionFrame()
23 {
24 super( "Multiple Selection Lists" );
25 setLayout( new FlowLayout() ); // set frame layout
27 colorJList = new JList( colorNames ); // holds names of all colors
28 colorJList.setVisibleRowCount( 5 ); // show five rows
29 colorJList.setSelectionMode( 
31 add( new JScrollPane( colorJList ) ); // add list with scrollpane
33 copyJButton = new JButton( "Copy >>>" ); // create copy button
34 copyJButton.addActionListener(
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
47 add( copyJButton ); // add copy button to JFrame
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( 
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:
 2 // Testing MultipleSelectionFrame.
 3 import javax.swing.JFrame;
 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.

Introduction to Computers, the Internet and the World Wide Web

Introduction to Java Applications

Introduction to Classes and Objects

Control Statements: Part I

Control Statements: Part 2

Methods: A Deeper Look


Classes and Objects: A Deeper Look

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

GUI Components: Part 1

Graphics and Java 2D™

Exception Handling

Files and Streams


Searching and Sorting

Data Structures



Introduction to Java Applets

Multimedia: Applets and Applications

GUI Components: Part 2



Accessing Databases with JDBC


JavaServer Pages (JSP)

Formatted Output

Strings, Characters and Regular Expressions

Appendix A. Operator Precedence Chart

Appendix B. ASCII Character Set

Appendix C. Keywords and Reserved Words

Appendix D. Primitive Types

Appendix E. (On CD) Number Systems

Appendix F. (On CD) Unicode®

Appendix G. Using the Java API Documentation

Appendix H. (On CD) Creating Documentation with javadoc

Appendix I. (On CD) Bit Manipulation

Appendix J. (On CD) ATM Case Study Code

Appendix K. (On CD) Labeled break and continue Statements

Appendix L. (On CD) UML 2: Additional Diagram Types

Appendix M. (On CD) Design Patterns

Appendix N. Using the Debugger

Inside Back Cover

Java(c) How to Program
Java How to Program (6th Edition) (How to Program (Deitel))
ISBN: 0131483986
EAN: 2147483647
Year: 2003
Pages: 615 © 2008-2020.
If you may any questions please contact us: