Java JCheckBoxMenuItem Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Java JCheckBoxMenuItem is a GUI component in Swing that extends the JMenuItem class. It represents a Menu item with a checkbox that can be checked or unchecked. JCheckBoxMenuItem is often used in menu systems to allow users to toggle options. In this article, we are going to explore some constructors, methods, and examples of JCheckBoxMenuItem. Constructors of JCheckBoxMenuItem ClassConstructors Descriptions JCheckBoxMenuItem() It is a default constructor, that creates a JCheckBoxMenuItem with no text and no icon. JCheckBoxMenuItem(Icon icon) It will add an icon to the JCheckBoxMenuItem. JCheckBoxMenuItem(String text) It will add a label with the specified text. JCheckBoxMenuItem(String text, Icon icon) This constructor creates a JCheckBoxMenuItem with both text and an icon. JCheckBoxMenuItem(String text, boolean selected) This constructor creates a JCheckBoxMenuItem with the specified text and an initial selected state JCheckBoxMenuItem(Action act) It uses the action object when a text or icon is selected. Methods of JCheckBoxMenuItem ClassMethods Descriptions boolean isSelected() Method for Returning whether the checkbox menu item is currently selected or not. void setSelected(boolean b) Sets the selected state of the checkbox menu item. True to check the item and False to uncheck it. void addItemListener(ItemListener l) Method for Adding an ItemListener to the checkbox menu item, which allows you to respond to changes in the item's state. protected String paramString() Method for returning a string representation of this JCheckBoxMenuItem. void setIcon(Icon icon) It will set an Icon to the JCheckBoxMenuItem. boolean getState() Method for returning whether the Item is selected or not. Programs to implement JCheckBoxMenuItemExample 1:Below is the implementation of the above topic: Java // Java Program to demonstrate a simple JCheckBoxMenuItem import javax.swing.*; // Driver Class public class JCheckBoxMenuItemExample { // main function public static void main(String[] args) { // Create a JFrame (window) with a title JFrame frame = new JFrame("JCheckBoxMenuItem Example"); // Set the default close operation to exit the // application when the window is closed frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Set the initial size of the window frame.setSize(300, 200); // Create a menu bar to hold menus JMenuBar menuBar = new JMenuBar(); // Create a menu labeled "Options" JMenu optionsMenu = new JMenu("Options"); // Create a JCheckBoxMenuItem with the label JCheckBoxMenuItem showGridItem1 = new JCheckBoxMenuItem("Option 1"); JCheckBoxMenuItem showGridItem2 = new JCheckBoxMenuItem("Option 2"); JCheckBoxMenuItem showGridItem3 = new JCheckBoxMenuItem("Option 3"); JCheckBoxMenuItem showGridItem4 = new JCheckBoxMenuItem("Option 4"); // Add the JCheckBoxMenuItem to the "Options" menu optionsMenu.add(showGridItem1); optionsMenu.add(showGridItem2); optionsMenu.add(showGridItem3); optionsMenu.add(showGridItem4); // Add the "Options" menu to the menu bar menuBar.add(optionsMenu); // Set the menu bar for the frame frame.setJMenuBar(menuBar); // Make the frame visible frame.setVisible(true); } } Output: Example 2:Below is the implementation of the above topic: Java // Java Program to Implementing ActionListener to the JCheckBoxMenuItem import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; // Driver Class public class CheckBoxMenuItemExample { // main function public static void main(String[] args) { JFrame frame = new JFrame("JCheckBoxMenuItem Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 200); JMenuBar menuBar = new JMenuBar(); JMenu optionsMenu = new JMenu("Options"); // Create JCheckBoxMenuItems with text JCheckBoxMenuItem showGridItem1 = new JCheckBoxMenuItem("Option 1"); JCheckBoxMenuItem showGridItem2 = new JCheckBoxMenuItem("Option 2"); JCheckBoxMenuItem showGridItem3 = new JCheckBoxMenuItem("Option 3"); JCheckBoxMenuItem showGridItem4 = new JCheckBoxMenuItem("Option 4"); // Create a JTextField to display the selected state JTextField textField = new JTextField(20); // Making it non editable textField.setEditable(false); // Create a common ActionListener to respond to changes in the checkbox state ActionListener checkBoxListener = new ActionListener() { public void actionPerformed(ActionEvent e) { JCheckBoxMenuItem selectedMenuItem = (JCheckBoxMenuItem) e.getSource(); String optionText = selectedMenuItem.getText(); if (selectedMenuItem.isSelected()) { textField.setText(optionText + " is selected."); } else { textField.setText(optionText + " is unselected."); } } }; // Add the ActionListener to all the JCheckBoxMenuItems showGridItem1.addActionListener(checkBoxListener); showGridItem2.addActionListener(checkBoxListener); showGridItem3.addActionListener(checkBoxListener); showGridItem4.addActionListener(checkBoxListener); //Add all the JCheckBoxMenuItem to the optionsMenu optionsMenu.add(showGridItem1); optionsMenu.add(showGridItem2); optionsMenu.add(showGridItem3); optionsMenu.add(showGridItem4); menuBar.add(optionsMenu); // Create a panel to hold the JTextField JPanel panel = new JPanel(); panel.add(textField); frame.setJMenuBar(menuBar); frame.add(panel, BorderLayout.CENTER); frame.setVisible(true); } } Output:Step 1: Option to be selected Step 2: Option can be deselected Step 3: Option is Selected(ActionListener Detected the Option 4) Comment More infoAdvertise with us Next Article Java JComponent C chinmaya121221 Follow Improve Article Tags : Java Geeks Premier League java-swing Geeks Premier League 2023 Practice Tags : Java Similar Reads Java JComponent In Java Swing, JComponent is an abstract class that programmers can use to modify to build unique components that are suited to the particular requirements of their applications. JComponent and other Swing components are lighter than their AWT equivalents. The Java runtime renders lightweight compon 6 min read Java AWT MenuItem & Menu Java Abstract Window Toolkit (AWT) provides a comprehensive set of classes and methods to create graphical user interfaces. In this article, we will explore the MenuItem and Menu classes, which are essential for building menus in Java applications. Java AWT MenuItemMenuItem is a class that represent 3 min read Java Swing | JCheckBox with examples JCheckBox is a part of Java Swing package . JCheckBox can be selected or deselected . It displays it state to the user . JCheckBox is an implementation to checkbox . JCheckBox inherits JToggleButton class. Constructor of the class are : JCheckBox() : creates a new checkbox with no text or icon JChec 5 min read Java JDesktopPane Java JDesktopPane is a part of the Swing library that helps Java Developers to make desktop-like applications, It allows the developers to manage and organize child frames within a parent frame. In this article, we are going to see some constructors, methods, fields, and some examples of JDesktopPan 4 min read Java JTextPane In Java, JTextPane is a versatile component that is part of the Swing library for building graphical user interfaces. It extends JEditorPane and provides an editable text component with rich text formatting capabilities. JTextPane allows you to display and edit styled text, making it suitable for im 3 min read Java JTabbedPane JTabbedPane is a GUI(Graphical User Interface) component in the Java Swing library that allows you to create a tabbed pane interface. A tabbed pane is a container that can store and organize multiple components into distinct tabs. It contains a collection of tabs. When you click on a tab, only data 5 min read Like