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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

67 application.setDefaultCloseOperation(<br />

68 JFrame.EXIT_ON_CLOSE );<br />

69 }<br />

70<br />

71 } // end class ComboBoxTest<br />

A scrollbar <strong>to</strong> scroll through<br />

the items in the list.<br />

Fig. Fig. 12.13 12.13 <strong>Program</strong> that uses a JComboBox <strong>to</strong> select an icon (part 3 of 3).<br />

Look-and-Feel Observation 12.7<br />

Set the maximum row count for a JComboBox <strong>to</strong> a number of rows that prevents the list from<br />

expanding outside the bounds of the window or applet in which it is used. This will ensure<br />

that the list displays correctly when it is expanded by the user. 12.7<br />

Lines 34–50 register an instance of an anonymous inner class that implements Item-<br />

Listener as the listener for JComboBox images. When the user makes a selection<br />

from images, method itemStateChanged (line 40–46) sets the Icon for label.<br />

The Icon is selected from array icons by determining the index number of the selected<br />

item in the JComboBox with method getSelectedIndex in line 45. Note that line 43<br />

changes the icon only for a selected item. The reason for the if structure here is that for<br />

each item that is selected from a JComboBox, another item is deselected. Thus, two events<br />

occur for each item selected. We wish <strong>to</strong> display only the icon for the item the user just<br />

selected.<br />

12.9 JList<br />

scroll arrows scroll box<br />

A list displays a series of items from which the user may select one or more items. Lists are<br />

created with class JList, which inherits from class JComponent. Class JList supports<br />

single-selection lists (i.e., lists that allow only one item <strong>to</strong> be selected at a time) and<br />

multiple-selection lists (lists that allow any number of items <strong>to</strong> be selected). In this section,<br />

we discuss single-selection lists.<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!