Java Reference
In-Depth Information
Check Boxes
A check box is a component that is either selected or deselected. When selected,
a check mark (or an x) appears in the box. A check box has a label associated
with it that typically appears to the right of the check box. The java.awt.Check-
box class represents an AWT check box, and the javax.swing.JCheckBox class
represents a Swing check box.
AWT Check Boxes
The java.awt.Checkbox class has five constructors, but two of them are for
creating radio buttons (discussed in the next section). The three constructors
for creating a check box are:
public Checkbox(String label). Creates a check box with the given label.
The initial state of the check box is deselected.
public Checkbox(String label, boolean selected). Creates a check box with
the given label that is initially selected if the boolean argument is true.
public Checkbox().
Creates a check box with no label that is initially
deselected.
The methods in Checkbox related to event handling include:
public void addItemListener(ItemListener i).
Generates an ItemEvent
when they are clicked by the user.
public void setState(boolean state) and public boolean getState(). For
changing or determining the state of the check box. The state is true
when the check box is selected and false when it is deselected.
The java.awt.event.ItemEvent class represents an item event. When a
check box is clicked, an ItemEvent is instantiated and passed in to the
itemStateChanged() method from the ItemListener interface (the only
method in ItemListener).
The method in the ItemEvent class that is typically of interest to the event
handler returns the type of state change. The possible return values are
ItemEvent.SELECTED or ItemEvent.DESELECTED.
public int getStateChange().
For example, when a user clicks a check box so that a check mark appears
in the box, the getStateChange() method returns ItemEvent.SELECTED. The
getItem() method of ItemEvent is also useful. For an AWT check box, it
returns the label of the Checkbox. For a Swing check box, it returns a
reference to the JCheckBox that was clicked.
Search WWH ::




Custom Search