Java Reference
In-Depth Information
// Paste
JMenuItem pasteMenuItem = new JMenuItem("Paste");
pasteMenuItem.addActionListener(actionListener);
pasteMenuItem.setEnabled(false);
popupMenu.add(pasteMenuItem);
// Separator
popupMenu.addSeparator();
// Find
JMenuItem findMenuItem = new JMenuItem("Find");
findMenuItem.addActionListener(actionListener);
popupMenu.add(findMenuItem);
JButton label = new JButton();
frame.add(label);
label.setComponentPopupMenu(popupMenu);
frame.setSize(350, 250);
frame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
}
JCheckBoxMenuItem Class
Swing's JCheckBoxMenuItem component behaves as if you have a JCheckBox on a menu as a
JMenuItem . The data model for the menu item is the ToggleButtonModel , described in Chapter 5.
It allows the menu item to have a selected or unselected state, while showing an appropriate
icon for the state. Because the data model is the ToggleButtonModel , when JCheckBoxMenuItem
is placed in a ButtonGroup , only one component in the group is ever selected. However, this
isn't the natural way to use a JCheckBoxMenuItem and is likely to confuse users. If you need this
behavior, use JRadioButtonMenuItem , as described later in this chapter.
Creating JCheckBoxMenuItem Components
There are seven constructors for JCheckBoxMenuItem . They allow you to initialize the text label,
icon, and initial state.
public JCheckBoxMenuItem()
JCheckBoxMenuItem jCheckBoxMenuItem = new JCheckBoxMenuItem();
public JCheckBoxMenuItem(String text)
JCheckBoxMenuItem jCheckBoxMenuItem = new JCheckBoxMenuItem("Boy");
Search WWH ::




Custom Search