Java Reference
In-Depth Information
Lines 19-23 (Fig. 12.21) declare and initialize array icons with four new ImageIcon
objects. String array names (lines 17-18) contains the names of the four image files that
are stored in the same directory as the application.
At line 31, the constructor initializes a JComboBox object with the String s in array
names as the elements in the list. Each item in the list has an index . The first item is added
at index 0, the next at index 1 and so forth. The first item added to a JComboBox appears
as the currently selected item when the JComboBox is displayed. Other items are selected
by clicking the JComboBox , then selecting an item from the list that appears.
Line 32 uses JComboBox method setMaximumRowCount to set the maximum number
of elements that are displayed when the user clicks the JComboBox . If there are additional
items, the JComboBox provides a scrollbar (see the first screen) that allows the user to scroll
through all the elements in the list. The user can click the scroll arrows at the top and
bottom of the scrollbar to move up and down through the list one element at a time, or
else drag the scroll box in the middle of the scrollbar up and down. To drag the scroll box,
position the mouse cursor on it, hold the mouse button down and move the mouse. In
this example, the drop-down list is too short to drag the scroll box, so you can click the up
and down arrows or use your mouse's wheel to scroll through the four items in the list.
Line 49 attaches the JComboBox to the ComboBoxFrame 's FlowLayout (set in line 29). Line
50 creates the JLabel that displays ImageIcon s and initializes it with the first ImageIcon
in array icons . Line 51 attaches the JLabel to the ComboBoxFrame 's FlowLayout .
Look-and-Feel Observation 12.12
Set the maximum row count for a JComboBox to a number of rows that prevents the list
from expanding outside the bounds of the window in which it's used.
Using an Anonymous Inner Class for Event Handling
Lines 34-46 are one statement that declares the event listener's class, creates an object of
that class and registers it as imagesJComboBox 's ItemEvent listener. This event-listener ob-
ject is an instance of an anonymous inner class —a class that's declared without a name
and typically appears inside a method declaration. As with other inner classes, an anonymous
inner class can access its top-level class's members. However, an anonymous inner class has
limited access to the local variables of the method in which it's declared. Since an anony-
mous inner class has no name, one object of the class must be created at the point where
the class is declared (starting at line 35).
Software Engineering Observation 12.3
An anonymous inner class declared in a method can access the instance variables and
methods of the top-level class object that declared it, as well as the method's final local
variables, but cannot access the method's non- final local variables. As of Java SE 8,
anonymous inner classes may also access a methods “effectively final ” local variables—see
Chapter 17 for more information.
Lines 34-47 are a call to imagesJComboBox 's addItemListener method. The argu-
ment to this method must be an object that is an ItemListener (i.e., any object of a class
that implements ItemListener ). Lines 35-46 are a class-instance creation expression that
declares an anonymous inner class and creates one object of that class. A reference to that
object is then passed as the argument to addItemListener . The syntax ItemListener()
 
Search WWH ::




Custom Search