Java Reference
In-Depth Information
Combining AWT and Swing Components
Do not combine complex AWT components—such as Menus and
Panels—with Swing components in the same container. Heavy-
weight AWT components paint on top of lightweight Swing
components.
You learned already that implementing ActionListener allows the program to
listen for a click event. You cannot compile the program yet because programs
implementing the ActionListener must include an actionPerformed() event
before they will compile successfully. JMenu and JComboBox components,
coded later in the program, will require clicks by the user, thereby making the
ActionListener necessary.
Constructing Class-Level Components
Components that are declared and constructed at the beginning of the class and
that are external to any other method are class-level components, which become
visible to all other methods within the class. The Classics on DVD program will
contain three Swing components and three String arrays that will have class-level
scope.
Constructing Swing Components
The three Swing components that will be constructed as class-level
components are JLabel, JComboBox, and JTextPane. A JLabel will be used to
display the words, Sort by:, at the top of the interface. A JLabel is similar to an
AWT Label in that it displays information in a GUI application. JLabels, how-
ever, also can display borders and images.
A JComboBox , Java's Swing version of a drop-down list, will display the sort
fields from which users may choose. A combo box is a drop-down list that
displays when users click a box arrow in the interface. A JComboBox can have
two different forms. The first form is the uneditable combo box , which features
a button and a drop-down list of values. The button displays a word and an
arrow; when a user clicks the button, its drop-down list displays. The second
form, the editable combo box , features a text box with a box arrow. This type
of combo box allows the user to type a value in the text box or click the box
arrow to display a drop-down list. The setEditable() method is used to set the
JComboBox to one of the two forms. The uneditable combo box is the default.
Figure 7-9 displays the two forms of the JComboBox.
text box
box arrow
button
uneditable
combo box
editable
combo box
FIGURE 7-9
 
Search WWH ::




Custom Search