JList

A list displays a series of items from which the user may select one or more items (see output of Fig. 11.23). Lists are created with class JList, which directly extends class JComponent. Class JList supports single-selection lists (which allow only one item to be selected at a time) and multiple-selection lists (which allow any number of items to be selected). In this section, we discuss single-selection lists.

Figure 11.23. JList that displays a list of colors.

(This item is displayed on pages 546 - 547 in the print version)

 1 // Fig. 11.23: ListFrame.java
 2 // Selecting colors from a JList.
 3 import java.awt.FlowLayout;
 4 import java.awt.Color;
 5 import javax.swing.JFrame;
 6 import javax.swing.JList;
 7 import javax.swing.JScrollPane;
 8 import javax.swing.event.ListSelectionListener;
 9 import javax.swing.event.ListSelectionEvent;
10 import javax.swing.ListSelectionModel;
11
12 public class ListFrame extends JFrame
13 {
14 private JList colorJList; // list to display colors
15 private final String colorNames[] = { "Black", "Blue", "Cyan",
16 "Dark Gray", "Gray", "Green", "Light Gray", "Magenta",
17 "Orange", "Pink", "Red", "White", "Yellow" };
18 private final Color colors[] = { Color.BLACK, Color.BLUE, Color.CYAN,
19 Color.DARK_GRAY, Color.GRAY, Color.GREEN, Color.LIGHT_GRAY,
20 Color.MAGENTA, Color.ORANGE, Color.PINK, Color.RED, Color.WHITE,
21 Color.YELLOW };
22
23 // ListFrame constructor add JScrollPane containing JList to JFrame
24 public ListFrame()
25 {
26 super( "List Test" );
27 setLayout( new FlowLayout() ); // set frame layout
28
29 colorJList = new JList( colorNames ); // create with colorNames 
30 colorJList.setVisibleRowCount( 5 ); // display five rows at once
31
32 // do not allow multiple selections 
33 colorJList.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
34
35 // add a JScrollPane containing JList to frame
36 add( new JScrollPane( colorJList ) ); 
37
38 colorJList.addListSelectionListener(
39 new ListSelectionListener() // anonymous inner class
40 {
41 // handle list selection events
42 public void valueChanged( ListSelectionEvent event )
43 {
44 getContentPane().setBackground(
45 colors[ colorJList.getSelectedIndex() ] );
46 } // end method valueChanged
47 } // end anonymous inner class
48 ); // end call to addListSelectionListener
49 } // end ListFrame constructor
50 } // end class ListFrame

The application of Fig. 11.23 and Fig. 11.24 creates a JList containing 13 color names. When a color name is clicked in the JList, a ListSelectionEvent occurs and the application changes the background color of the application window to the selected color. Class ListTest (Fig. 11.24) contains the main method that executes this application.

Figure 11.24. Test class for ListFrame.

(This item is displayed on pages 547 - 548 in the print version)

 1 // Fig. 11.24: ListTest.java
 2 // Selecting colors from a JList.
 3 import javax.swing.JFrame;
 4
 5 public class ListTest
 6 {
 7 public static void main( String args[] )
 8 {
 9 ListFrame listFrame = new ListFrame(); // create ListFrame
10 listFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
11 listFrame.setSize( 350, 150 ); // set frame size
12 listFrame.setVisible( true ); // display frame
13 } // end main
14 } // end class ListTest
 

Line 29 (Fig. 11.23) creates JList object colorList. The argument to the JList constructor is the array of Objects (in this case Strings) to display in the list. Line 30 uses JList method setVisibleRowCount to determine the number of items that are visible in the list.

Line 33 uses JList method setSelectionMode to specify the list's selection mode. Class ListSelectionModel (of package javax.swing) declares three constants that specify a JList's selection modeSINGLE_SELECTION (which allows only one item to be selected at a time), SINGLE_INTERVAL_SELECTION (for a multiple-selection list that allows selection of several contiguous items) and MULTIPLE_INTERVAL_SELECTION (for a multiple-selection list that does not restrict the items that can be selected).

Unlike a JComboBox, a JList does not provide a scrollbar if there are more items in the list than the number of visible rows. In this case, a JScrollPane object is used to provide the scrolling capability. Line 36 adds a new instance of class JScrollPane to the JFrame. The JScrollPane constructor receives as its argument the JComponent that needs scrolling functionality (in this case, colorList). Notice in the screen captures that a scrollbar created by the JScrollPane appears at the right side of the JList. By default, the scrollbar appears only when the number of items in the JList exceeds the number of visible items.

Lines 3848 use JList method addListSelectionListener to register an object that implements ListSelectionListener (package javax.swing.event) as the listener for the JList's selection events. Once again, we use an instance of an anonymous inner class (lines 3947) as the listener. In this example, when the user makes a selection from colorList, method valueChanged (line 4246) should change the background color of the ListFrame to the selected color. This is accomplished in lines 4445. Note the use of JFrame method getContentPane on line 44. Each JFrame actually consists of three layersthe background, the content pane and the glass pane. The content pane appears in front of the background and is where the GUI components in the JFrame are displayed. The glass pane is used to display tool tips and other items that should appear in front of the GUI components on the screen. The content pane completely hides the background of the JFrame; thus, to change the background color behind the GUI components, you must change the content pane's background color. Method getContentPane returns a reference to the JFrame's content pane (an object of class Container). In line 44, we then use that reference to call method setBackground, which sets the content pane's background color to an element in the colors array. The color is selected from the array by using the selected item's index. JList method getSelectedIndex returns the selected item's index. As with arrays and JComboBoxes, JList indexing is zero based.





Java(c) How to Program
Java How to Program (6th Edition) (How to Program (Deitel))
ISBN: 0131483986
EAN: 2147483647
Year: 2003
Pages: 615
Simiral book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net