Java Swing Tutorials - Herong's Tutorial Examples - Version 4.10, by Dr. Herong Yang - Check Box Menu Item Test Program

This section provides a tutorial example on how to use the javax.swing.JCheckBoxMenuItemTest class to create check box menu items. If a check box menu item is selected, its check box icon will have a check sign.

Swing also supports a special menu item class, javax.swing.JCheckBoxMenuItemTest, which represents check box menu items with following special features:

  • A check box menu item will be listed with a check box icon.
  • The check box icon will be displayed as checked if the check box menu item is selected.
  • The check box icon will be displayed as unchecked if the check box menu item is unselected.

Here is an example program I wrote to test the JCheckBoxMenuItemTest classes:

 * Copyright (c) 2014,, All Rights Reserved.
import javax.swing.*;
public class JCheckBoxMenuItemTest {
   JFrame myFrame = null;
   public static void main(String[] a) {
      (new JCheckBoxMenuItemTest()).test();
   private void test() {
      myFrame = new JFrame("Check Box Menu Item Test");
      myFrame.setContentPane(new JDesktopPane());

      JMenuBar myMenuBar = new JMenuBar();
      JMenu myMenu = getFileMenu();
      myMenu = getColorMenu();
      myMenu = getOptionMenu();
      JMenuItem myItem = new JMenuItem("Help");

   private JMenu getFileMenu() {
      JMenu myMenu = new JMenu("File");
      JMenuItem myItem = new JMenuItem("Open");
      myItem = new JMenuItem("Close");
      myItem = new JMenuItem("Exit");
      return myMenu;
   private JMenu getColorMenu() {
      JMenu myMenu = new JMenu("Color");
      JMenuItem myItem = new JMenuItem("Red");
      myItem = new JMenuItem("Green");
      myItem = new JMenuItem("Blue");
      return myMenu;
   private JMenu getOptionMenu() {
      JMenu myMenu = new JMenu("Option");
      JCheckBoxMenuItem myItem = new JCheckBoxMenuItem("Sound");
      myItem = new JCheckBoxMenuItem("Auto save");
      return myMenu;

If you run this example, you will see the frame window shows up with the menu bar like this:
Check Box Menu Item Test

Interesting notes about this tutorial example:

  • A check box menu item can be listed as selected by default, if the setSelected(true) method is called on the item. The "Sound" check box menu item selected by default in this example.

Sample programs listed in this section have been tested with JDK 1.6.0 to 1.8.0.

Last update: 2014.

Table of Contents

 About This Java Swing Tutorial Book

 Introduction of Java Swing Package

 Graphics Environment of the Local System

 JFrame - Main Frame Class

 JLabel - Swing Label Class

 JButton - Swing Button Class

 JRadioButton - Swing Radio Button Class

 JTextField - Swing Text Field Class

Menu Bar, Menus, Menu Items and Listeners

 JMenuBar, JMenu, and JMenuItem Classes - Menu Bar Test Program - Menu Test Program - Menu Item Test Program - Radio Button Menu Item Test Program - Check Box Menu Item Test Program

 javax.swing.event.MenuListener - Menu Listener Interface - Menu Item Action Listener Test

 Item Listener on Radio Button Menu Items

 Item Listener on Check Box Menu Items

 javax.swing.event.MenuKeyListener - Menu Key Listener Interface

 setMnemonic() - Setting Keyboard Mnemonics on Menu Items

 setAccelerator() - Setting Keyboard Accelerators on Menu Items

 setMnemonic() - Setting Keyboard Mnemonics on Menus

 Creating Internal Frames inside the Main Frame

 Layout of Components in a Container

 LookAndFeel and UIManager

 Option Dialog Boxes

 JEditorPane - The Editor Pane Class

 SwingWorker - The Background Task Worker


 Printable Copy - PDF Version - Check Box Menu Item Test Program - Updated in 2014, by Dr. Herong Yang