Java Reference
In-Depth Information
Listening to JCheckBoxMenuItem with an ItemListener
If you listen for JCheckBoxMenuitem selection with an ItemListener , you don't need to query the
event source for the selection state—the event already carries that information. Based on this
state, you respond accordingly. Re-creating the ActionListener behavior with an ItemListener
requires just a few minor changes to the previously listed source, as follows:
ItemListener iListener = new ItemListener() {
public void itemStateChanged(ItemEvent event) {
Icon girlIcon = new ImageIcon("girl-r.jpg");
Icon boyIcon = new ImageIcon("boy-r.jpg");
AbstractButton aButton = (AbstractButton)event.getSource();
int state = event.getStateChange();
String newLabel;
Icon newIcon;
if (state == ItemEvent.SELECTED) {
newLabel = "Girl";
newIcon = girlIcon;
} else {
newLabel = "Boy";
newIcon = boyIcon;
}
aButton.setText(newLabel);
aButton.setIcon(newIcon);
}
};
Customizing a JCheckBoxMenuItem Look and Feel
The appearance of the JCheckBoxMenuItem under the preinstalled set of look and feel types is
shown with the other menu components in Figure 6-3.
The available set of UIResource -related properties for a JCheckBoxMenuItem is shown in
Table 6-12. The JCheckBoxMenuItem component has 19 different properties.
Table 6-12. JCheckBoxMenuItem UIResource Elements
Property String
Object Type
CheckBoxMenuItem.acceleratorFont
Font
CheckBoxMenuItem.acceleratorForeground
Color
CheckBoxMenuItem.acceleratorSelectionForeground
Color
CheckBoxMenuItem.actionMap
ActionMap
CheckBoxMenuItem.arrowIcon
Icon
CheckBoxMenuItem.background
Color
CheckBoxMenuItem.border
Border
CheckBoxMenuItem.borderPainted
Boolean
Search WWH ::




Custom Search