SWING - Menu Classes-JCheckboxMenuItem Class - Java Swing

What is SWING JCheckboxMenuItem Class?

Introduction

The JCheckboxMenuItem class represents a check box which can be included in a menu. Selecting the check box in the menu changes the control's state from on to off or from off to on.

Class Declaration

Following is the declaration for javax.swing.JCheckBoxMenuItem class −

Class Constructors

Sr.No. Constructor & Description
1
JCheckboxMenuItem()
Creates an initially unselected check box menu item with no set text or icon.
2
JCheckboxMenuItem(Action a)
Creates a menu item whose properties are taken from the Action supplied.
3
JCheckboxMenuItem(Icon icon)
Creates an initially unselected check box menu item with an icon.
4
JCheckboxMenuItem(String text)
Creates an initially unselected check box menu item with text.
5
JCheckboxMenuItem(String text, boolean b)
Creates a check box menu item with the specified text and selection state.
6
JCheckboxMenuItem(String text, Icon icon)
Creates an initially unselected check box menu item with the specified text and icon.
7
JCheckboxMenuItem(String text, Icon icon, boolean b)
Creates a checkbox menu item with the specified text, icon, and selection state.

Class Methods

Sr.No. Method & Description
1
AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this JCheckBoxMenuItem.
2
Object[] getSelectedObjects()
Returns an array (length 1) containing the check box menu item label or null if the check box is not selected.
3
boolean getState()
Returns the selected-state of the item.
4
String getUIClassID()
Returns the name of the L&F class that renders this component.
5
protected String paramString()
Returns a string representation of this JCheckBoxMenuItem.
6
void setState(boolean b)
Sets the selected-state of the item.

Methods Inherited

This class inherits methods from the following classes −

  • javax.swing.JMenuItem
  • javax.swing.JAbstractButton
  • javax.swing.JComponent
  • java.awt.Container
  • java.awt.Component
  • java.lang.Object

JCheckboxMenuItem Example

Create the following Java program using any editor of your choice in say D:/ > SWING > com > wisdomjobs> gui >

SwingMenuDemo.java

Compile the program using the command prompt. Go to D:/ > SWING and type the following command.

If no error occurs, it means the compilation is successful. Run the program using the following command.

Verify the following output. (Click on File Menu. Unselect "Show About" menu item.)

swing_jcheckboxmenuitem

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Java Swing Topics