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;
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 };
23 // ListFrame constructor add JScrollPane containing JList to JFrame
24 public ListFrame()
25 {
26 super( "List Test" );
27 setLayout( new FlowLayout() ); // set frame layout
29 colorJList = new JList( colorNames ); // create with colorNames 
30 colorJList.setVisibleRowCount( 5 ); // display five rows at once
32 // do not allow multiple selections 
33 colorJList.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
35 // add a JScrollPane containing JList to frame
36 add( new JScrollPane( colorJList ) ); 
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;
 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.

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

show all menu

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

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