Java Reference
In-Depth Information
Table 4-15
The addItemListener Event
Syntax:
component.addItemListener(ItemListener object)
Comment:
Causes the applet to listen for clicks initiated by the user. The
component must be declared with a constructor before
triggering the event. The ItemListener object may be self-
referential, using the argument, this, or a constructor of a new
ItemListener object.
Example:
optBlue.addItemListener(this);
Applets can use graphics and color to keep users interested and provide
ease of use. Two methods help you change the color of your applet: the
setBackground() method and the setForeground() method. As you learned in
Chapter 2, the setBackground() method changes the background color of the
applet window or other component. The setForeground() method changes the
color of the text used in the applet window. You may want to change the fore-
ground (text) color to draw attention to a certain component or use a lighter
color to make the text display more clearly on darker backgrounds. Table 4-16
displays the general form of the setBackground() and setForeground() methods.
Table 4-16
The setBackground() and setForeground() methods
METHOD
PURPOSE
EXAMPLE
setBackground()
Set the background color
1. setBackground(Color.blue);
of an applet or other component
2. myLabel.setBackground(Color.cyan);
setForeground()
Set the foreground color
3. setForeground(darkRed);
of an applet or other component
4. myTextField.setForeground(Color.cyan);
Example 1 sets the background of the entire applet window to blue. If you
want to set the background color for a specific Label or Checkbox, you must pre-
cede the command with the name of the object, as shown in example 2.
The argument of both methods uses a Color object. In example 3, the color
darkRed was declared and assigned previously, so the attribute does not need to
be preceded by the name of the Color object. In example 4, the Color object is
followed by a period delimiter followed by a valid color attribute. The preset
color attributes used for most components are medium gray for the background
and black for the foreground or text. To review a list of valid attributes for the
Color object, see Table 2-9 on page 99.
Another method associated with applets that use TextFields is the
requestFocus() method. The requestFocus() method moves the insertion point
to the component that calls it. In the case of TextFields, when the TextField has
the focus, the insertion point displays as a vertical flashing line in the text box. A
command button, such as OK or Cancel, may display focus with a dotted rectan-
gle displayed around the button's caption. Displaying the insertion point helps
users focus on the appropriate spot to enter the next item of text and commonly
is used when clearing an incorrect entry to let the user try another entry.
 
Search WWH ::




Custom Search