Java Reference
In-Depth Information
Just as a button can be associated with an ActionListener, a checkbox can be assigned an ItemListener. To use
an ItemListener, simply implement the ItemListener in the class header, add the ItemListener to the checkbox,
and code an itemStateChanged method. The itemStateChanged method will be executed whenever the checkbox's
state is changed.
Checkboxes can also be grouped. Grouped checkboxes have a small round text field (like a Web page radio
button). The checkbox will be empty when unchecked and will have a small black circle when checked. Within a
checkbox group, only one checkbox can be in a checked state. If a checkbox in a group is checked, checking any
another checkbox will change the state of the original checkbox to unchecked.
To create a checkbox group, you must perform the follow steps:
A.
Import the following classes:
Checkbox
CheckboxGroup
ItemListener
ItemEvent
B.
Add an itemStateChanged method
C.
Implement the ItemListener
D.
Define at least one Checkbox object
E.
Add the ItemListener to the checkbox(es)
F.
Create a CheckboxGroup object
G.
Add the checkbox(es) to the checkbox group
H.
Add the checkbox(es) to the frame
Tutorial: Coding a Checkbox
Now let's code the checkbox:
1.
Using RAD, create a new Java class (not a visual class) in Tutorials/src/c6 called CBTestApp
as follows:
c6.UsefulFrame is the superclass;
an ItemListener interface is implemented; and
main and inherited abstract method stubs are created.
The source code should look like the following:
package c6;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
public class CBTestApp extends UsefulFrame implements ItemListener {
public void itemStateChanged(ItemEvent e) {
}
 
Search WWH ::




Custom Search