How can I add a check mark next to a menu item after it's been selected?

I have the following code which shows a menu called “Options” and under it are the menu items “AI Mode” and “Player Mode”. I just want to know how I can mark each one of these with a check mark when chosen.


package com.sean.breakout.menu;

import javax.swing.*;

public class MenuBar extends JMenuBar {
	
	private static final long serialVersionUID = 1L;
	
	public MenuBar() {
		add(createOptionsMenu());	
	}

	private JMenu createOptionsMenu() {
		JMenu fileOptions = new JMenu("Options");
		JMenuItem aiMode = new JMenuItem("AI Mode");
		JMenuItem playerMode = new JMenuItem("Player Mode");
		fileOptions.add(aiMode);
		fileOptions.add(playerMode);
		return fileOptions;
	}
	
}


you could use a http://docs.oracle.com/javase/7/docs/api/javax/swing/JCheckBoxMenuItem.html or http://docs.oracle.com/javase/7/docs/api/javax/swing/JRadioButtonMenuItem.html

Ok, but how can I make it so that if one box is checked, the other one is automatically unchecked?


package com.sean.breakout.menu;

import java.awt.event.*;
import javax.swing.*;

public class MenuBar extends JMenuBar {

private static final long serialVersionUID = 1L;

public MenuBar() {
    add(createOptionsMenu());
}

private JMenu createOptionsMenu() {
    JMenu fileOptions = new JMenu("Options");
    JCheckBoxMenuItem aiMode = new JCheckBoxMenuItem("AI Mode");
    aiMode.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }
    });
    JCheckBoxMenuItem playerMode = new JCheckBoxMenuItem("Player Mode");
    playerMode.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }
    });
    fileOptions.add(aiMode);
    fileOptions.add(playerMode);
    return fileOptions;
}
}

Use basil_'s second link. The concept of radio buttons is what you want.

Got it. Thanks.