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 the 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 between adjoining JCheckBoxMenuItem objects— the user can check any item without affecting the state of the others. Figure 14.14 shows the class diagram for the JCheckBoxMenuItem component.

JCheckBoxMenuItem class diagram

Figure 14-14. JCheckBoxMenuItem class diagram


Table 14.9 shows the properties of the JCheckBoxMenuItem class. JCheckBoxMenuItem inherits the JMenuItem model (ButtonModel) and its accessors. The JCheckBoxMenuItem class also contains two additional component properties. The state property has the value true if the menu item is currently in the checked state and false if it is not. The setState() accessor is synchronized. The selectedObjects 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 ...

Get Java Swing now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.