Java Swing How to - Initialize JCheckBoxMenuItem with checked option








Question

We would like to know how to initialize JCheckBoxMenuItem with checked option.

Answer

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//from   w ww  .  ja v a  2 s .co  m
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JSeparator;
import javax.swing.JTextField;

public class Main extends JFrame {
  Container frameContainer;
  JTextField textField = new JTextField(50);
  JMenuBar menuBar = new JMenuBar();
  JMenu fileMenu = new JMenu("File");

  JMenuItem fileNew = new JMenuItem("New");
  JMenuItem fileOpen = new JMenuItem("Open");
  JMenuItem fileSave = new JMenuItem("Save");
  JMenuItem fileExit = new JMenuItem("Exit");

  JMenu editMenu = new JMenu("Edit");
  JMenuItem editCut = new JMenuItem("Cut");
  JMenuItem editCopy = new JMenuItem("Copy");
  JMenuItem editPaste = new JMenuItem("Paste");

  JMenu specialMenu = new JMenu("Special");

  JCheckBoxMenuItem specialCheck1 = new JCheckBoxMenuItem("Check 1");
  JCheckBoxMenuItem specialCheck2 = new JCheckBoxMenuItem("Check 2", true);

  JSeparator separator = new JSeparator();

  JRadioButtonMenuItem specialRadio1 = new JRadioButtonMenuItem("Radio 1");

  JRadioButtonMenuItem specialRadio2 = new JRadioButtonMenuItem("Radio 2");

  ButtonGroup buttonGroup = new ButtonGroup();

  public Main() {
    fileMenu.add(fileNew);
    fileMenu.add(fileOpen);
    fileMenu.add(fileSave);
    fileMenu.add(fileExit);
    editMenu.add(editCut);
    editMenu.add(editCopy);
    editMenu.add(editPaste);
    specialMenu.add(specialCheck1);
    specialMenu.add(specialCheck2);
    specialMenu.add(separator);
    buttonGroup.add(specialRadio1);
    buttonGroup.add(specialRadio2);
    specialMenu.add(specialRadio1);
    specialMenu.add(specialRadio2);
    menuBar.add(fileMenu);
    menuBar.add(editMenu);
    menuBar.add(specialMenu);
    setJMenuBar(menuBar);

    frameContainer = getContentPane();
    textField.setBounds(100, 100, 100, 20);
    frameContainer.add(textField);

    setSize(450, 450);
    setVisible(true);
  }
  public static void main(String[] args) {
    Main app = new Main();
  }
}