Java JCheckBoxMenuItem
JCheckBoxMenuItem class represents checkbox which can be included on a menu . A
CheckBoxMenuItem can have text or a graphic icon or both, associated with
it. MenuItem can be selected or deselected. MenuItems can be configured and
controlled by actions.
Nested class
Modifier Class Description
and Type
protected JCheckBoxMenuItem.AccessibleJCheckBoxMenuItem This class implements
class accessibility support for the
JcheckBoxMenuItem class.
Constructor
Constructor Description
JCheckBoxMenuItem() It creates an initially unselected check box menu
item with no set text or icon.
JCheckBoxMenuItem(Action a) It creates a menu item whose properties are taken
from the Action supplied.
JCheckBoxMenuItem(Icon icon) It creates an initially unselected check box menu
item with an icon.
JCheckBoxMenuItem(String text) It creates an initially unselected check box menu
item with text.
JCheckBoxMenuItem(String text, boolean It creates a check box menu item with the specified
b) text and selection state.
JCheckBoxMenuItem(String text, Icon It creates an initially unselected check box menu
icon) item with the specified text and icon.
JCheckBoxMenuItem(String text, Icon It creates a check box menu item with the specified
icon, boolean b) text, icon, and selection state.
Methods
Modifier Method Description
AccessibleContex getAccessibleContext( It gets the AccessibleContext associated with this
t ) JCheckBoxMenuItem.
Object[] getSelectedObjects() It returns an array (length 1) containing the check
box menu item label or null if the check box is not
selected.
boolean getState() It returns the selected-state of the item.
String getUIClassID() It returns the name of the L&F class that renders
this component.
protected String paramString() It returns a string representation of this
JCheckBoxMenuItem.
void setState(boolean b) It sets the selected-state of the item.
Java JCheckBoxMenuItem Example
1. import java.awt.event.ActionEvent;
2. import java.awt.event.ActionListener;
3. import java.awt.event.KeyEvent;
4. import javax.swing.AbstractButton;
5. import javax.swing.Icon;
6. import javax.swing.JCheckBoxMenuItem;
7. import javax.swing.JFrame;
8. import javax.swing.JMenu;
9. import javax.swing.JMenuBar;
10. import javax.swing.JMenuItem;
11.
12. public class JavaCheckBoxMenuItem {
13. public static void main(final String args[]) {
14. JFrame frame = new JFrame("Jmenu Example");
15. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
16. JMenuBar menuBar = new JMenuBar();
17. // File Menu, F - Mnemonic
18. JMenu fileMenu = new JMenu("File");
19. fileMenu.setMnemonic(KeyEvent.VK_F);
20. menuBar.add(fileMenu);
21. // File->New, N - Mnemonic
22. JMenuItem menuItem1 = new JMenuItem("Open", KeyEvent.VK_N);
23. fileMenu.add(menuItem1);
24.
25. JCheckBoxMenuItem caseMenuItem = new JCheckBoxMenuItem("Option_1");
26. caseMenuItem.setMnemonic(KeyEvent.VK_C);
27. fileMenu.add(caseMenuItem);
28.
29. ActionListener aListener = new ActionListener() {
30. public void actionPerformed(ActionEvent event) {
31. AbstractButton aButton = (AbstractButton) event.getSource();
32. boolean selected = aButton.getModel().isSelected();
33. String newLabel;
34. Icon newIcon;
35. if (selected) {
36. newLabel = "Value-1";
37. } else {
38. newLabel = "Value-2";
39. }
40. aButton.setText(newLabel);
41. }
42. };
43.
44. caseMenuItem.addActionListener(aListener);
45. frame.setJMenuBar(menuBar);
46. frame.setSize(350, 250);
47. frame.setVisible(true);
48. }
49. }
Output: