Java Reference
In-Depth Information
Figure 13.4
Selecting red and blue creates a purple window.
Swing Check Boxes
Now, let's look at Swing check boxes. The javax.swing.JCheckBox class has
several constructors, each of which takes in a combination of the following
three parameters:
String label. For the label of the check box.
Icon icon. For an icon associated with the check box.
boolean selected.
For denoting the initial state of the check box.
A JCheckBox is a source of both an ItemEvent and also a ChangeEvent, both
of which are generated when the check box is clicked on by the user. The
ItemEvent can be used to determine the current state of the check box, while
the ChangeEvent only denotes that the state has changed.
Creating and laying out Swing check boxes is essentially identical to doing
so for AWT check boxes; however, some minor modifications are needed
for our ItemListener. For an AWT check box, invoking getItem() on the
ItemEvent returns the label of the check box. With Swing check boxes,
invoking getItem() returns a reference to the check box.
The MixSwingColors class found on the Web site is similar to the
MixColors listener, except that it works for JCheckBox components instead
of Checkbox components. The MixSwingColors listener is used in the
JCheckBoxDemo program.
Search WWH ::




Custom Search