Selectable Menu Items
So far, we’ve covered traditional menu items that produce a simple text-oriented label. But that’s not the only type of item users are used to seeing. Swing provides for two selectable menu items: the check box menu item and the radio button menu item.
The JCheckBoxMenuItem Class
Check box menu items are represented by
JCheckBoxMenuItem class. As you might have
guessed, this object behaves similarly to the
JCheckBox object. By clicking on a check box menu
item, you can toggle a UI-defined checkmark that generally appears to
the left of the menu item’s label. There is no mutual exclusion
the user can check any item without affecting the state of the
others. Figure 14.14 shows the class diagram for the
Figure 14-14. JCheckBoxMenuItem class diagram
Table 14.9 shows the properties of the
JCheckBoxMenuItem inherits the
JMenuItem model (
and its accessors. The
also contains two additional component properties. The
property has the value
true if the menu item is currently in the checked
false if it is not. The
accessor is synchronized.
property contains an
Object array of size one, consisting of the text of the menu item if it is currently in the checked state. If it is ...