26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

678 Graphical User Interface Components: Part 1 Chapter 12<br />

71 // execute application<br />

72 public static void main( String args[] )<br />

73 {<br />

74 MultipleSelection application = new MultipleSelection();<br />

75<br />

76 application.setDefaultCloseOperation(<br />

77 JFrame.EXIT_ON_CLOSE );<br />

78 }<br />

79<br />

80 } // end class MultipleSelection<br />

Fig. Fig. 12.15 12.15 Using a multiple-selection JList (part 3 of 3).<br />

Line 29 creates JList colorList and initializes it with the Strings in the array<br />

colorNames. Line 30 sets the number of visible rows in colorList <strong>to</strong> 5. Line 31 uses<br />

JList method setFixedCellHeight <strong>to</strong> specify the height in pixels of each item in<br />

the JList. We do this <strong>to</strong> ensure that the rows in both JLists in the example have the<br />

same height. Lines 32–33 specify that colorList is a MULTIPLE_INTER-<br />

VAL_SELECTION list. Line 34 adds a new JScrollPane containing colorList <strong>to</strong><br />

the content pane. Lines 59–65 perform similar tasks for JList copyList, which is<br />

defined as a SINGLE_INTERVAL_SELECTION list. Line 61 uses JList method set-<br />

FixedCellWidth <strong>to</strong> set copyList’s width <strong>to</strong> 100 pixels.<br />

A multiple-selection list does not have a specific event associated with making multiple<br />

selections. Normally, an event generated by another GUI component (known as an<br />

external event) specifies when the multiple selections in a JList should be processed. In<br />

this example, the user clicks JBut<strong>to</strong>n copyBut<strong>to</strong>n <strong>to</strong> trigger the event that copies the<br />

selected items in colorList <strong>to</strong> copyList.<br />

When the user clicks copyBut<strong>to</strong>n, method actionPerformed (line 45–50) is<br />

called. Lines 48–49 use JList method setListData <strong>to</strong> set the items displayed in<br />

copyList. Line 49 calls colorList’s method getSelectedValues, which returns<br />

an array of Objects representing the selected items in colorList. In this example, the<br />

returned array is passed as the argument <strong>to</strong> copyList’s setListData method.<br />

Many students ask how reference copyList can be used in line 48, when the program<br />

does not create the object <strong>to</strong> which it refers until Line 59. Remember that method action-<br />

Performed at lines 45–50 does not execute until the user presses the copyBut<strong>to</strong>n, which<br />

cannot occur until after the call <strong>to</strong> the construc<strong>to</strong>r completes. At that point in the program’s<br />

execution, line 59 already has initialized copyList with a new JList object.<br />

12.11 Mouse Event Handling<br />

This section presents the MouseListener and MouseMotionListener event-listener<br />

interfaces for handling mouse events. Mouse events can be trapped for any GUI com-<br />

© Copyright 1992–2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!