Open In App

Java JCheckBoxMenuItem

Last Updated : 13 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
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 Class

Constructors

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 Class

Methods

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 JCheckBoxMenuItem

Example 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:

Simple JCheckBoxMenuItem output image

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

JCheckBoxMenuItem Output with No Option Selected

Step 2: Option can be deselected

JCheckBoxMenuItem Output with Option 4 Selected

Step 3: Option is Selected(ActionListener Detected the Option 4)
Option 4 detected by ActionListener



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads