Java Reference
In-Depth Information
Note that on Unix systems, you have to escape the $ in the inner class name with
a backslash. On Windows systems, the backslash is not necessary. We'll see
another use of ItemChooser in Example 10-18.
Figure 10•12. A demonstration of the ItemChooser component
The interesting thing about ItemChooser is that it defines its own event and event
listener types as inner classes. You should pay attention to the definitions of these
types and study how they are used within the ItemChooser Demo classes, as this
example demonstrates both sides of the event architecture: event generation and
event handling. This example shows you how to work with JList , JComboBox , and
JRadioButton components; it is particularly interesting because it listens for and
responds to the events generated by those internal components and translates
those internal events into its own event type. Once you understand how Item-
Chooser works, you'll have a thorough understanding of the AWT and Swing event
architecture.
Example 10•15: ItemChooser.java
package com.davidflanagan.examples.gui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.util.*;
/**
* This class is a Swing component that presents a choice to the user. It
* allows the choice to be presented in a JList, in a JComboBox, or with a
* bordered group of JRadioButton components. Additionally, it displays the
* name of the choice with a JLabel. It allows an arbitrary value to be
* associated with each possible choice. Note that this component only allows
* one item to be selected at a time. Multiple selections are not supported.
**/
public class ItemChooser extends JPanel {
// These fields hold property values for this component
String name;
// The overall name of the choice
String[] labels;
// The text for each choice option
Search WWH ::




Custom Search