Java Reference
In-Depth Information
To Construct Applet Components
1. Enter lines 23 through 36 as shown in Figure 4-42 on the previous page.
TextPad displays the code to construct the components (Figure 4-43).The
Checkbox constructors use the variable, codeGroup, to include the four
Checkboxes as members of the CheckboxGroup named codeGroup.
constructor code
to add Labels, a
TextField, and
Checkboxes in a
CheckboxGroup
FIGURE 4-43
Table 4-14 displays some of the methods used with a Checkbox. For infor-
mation on other methods, the Java API contains a table associated with each
component.
Table 4-14
Methods Used with the Checkbox Component
METHOD
PURPOSE
EXAMPLE
setState()
To set or determine whether
boolean answer = myOption.getState();
getState()
the Checkbox is selected
setLabel()
To set or determine the
myOption.setLabel("This is the new caption");
getLabel()
caption of a Checkbox
addItemListener()
Allows the Checkbox to
myCheckbox.addItemListener();
become a trigger for an event
Adding Color, Components, Focus, and Listeners
to the Applet
You may recall that the add() method takes an argument of a declared
component and adds it to the Applet Viewer window when the applet is initi-
ated. The addItemListener event then causes the applet to listen for clicks
initiated by the user. When the click occurs, a series of associated objects and
methods change, including the getState() method, the itemStateChanged()
method, and the ItemEvent() object. Table 4-15 describes the general form of
the addItemListener event.
 
Search WWH ::




Custom Search