Geertjan's Blog

  • July 8, 2006

JCheckBoxMenuItem & JRadioButtonMenuItem (Part 1)

Geertjan Wielenga
Product Manager
Not all menu items are boring. Some have a check mark to indicate that the menu item has been selected...

...while others have a small black dot that performs the same function as the check mark:

The check mark is provided by the JCheckBoxMenuItem class, while the dot is there thanks to the JRadioButtonMenuItem class.

To add JCheckBoxMenuItems and JRadioButtonMenuItems to NetBeans modules or to applications based on the NetBeans Platform, implement the CallableSystemAction class (via the New Action wizard) and then override the getMenuPresenter() method. The cool thing is not so much the visual feedback via the check mark or dot, but the fact that you can make things happen depending on the state of the menu item (i.e., either selected or unselected).

For example, here's my class that extends CallableSystemAction, implemented for JCheckBoxMenuItem, but with comments explaining how to change the imlementation to make it work for JRadioButtonMenuItem:

public class MyAction extends CallableSystemAction {
private static JCheckBoxMenuItem abc;
//private static JRadioButtonMenuItem abc;
private static final String ICON_PATH = "org/netbeans/mdoules/checkboxitemsample/HOMER16.png";
private ImageIcon ICON = new ImageIcon(Utilities.loadImage(MyAction.ICON_PATH, true));
public JMenuItem getMenuPresenter() {
//Use the following line for JCheckBoxMenuItem:
abc = new JCheckBoxMenuItem("Open Homer Window", null);
//Use the following line for JRadioButtonMenuItem:
//abc = new JRadioButtonMenuItem("Open Homer Window", null);
//Use the following line for JCheckBoxMenuItem:
//Use the following line for JRadioButtonMenuItem:
//Here we listen and then act:
abc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TopComponent tc = MyTopComponent.findInstance().getDefault();
if(abc.isSelected()) {
if (!tc.isOpened()) {
} else {
if (tc.isOpened()) {
return abc;
//Required by CallableSystemAction:
public void performAction() {
//Required by CallableSystemAction:
public String getName() {
return null;
//Required by CallableSystemAction:
public HelpCtx getHelpCtx() {
return null;

And that's it. The important thing to note is that all of the CallableSystemAction methods are meaningless to us; everything has to be defined within the getMenuPresenter() method that overrides the default way that a menu item is implemented in NetBeans IDE (i.e., as a boring menu item). Now, when the menu item is selected, the window (which extends the TopComponent class, which can also be generated from a wizard in NetBeans IDE) opens and when it is unselected, the window closes. Could come in pretty handy sometimes.

Instead of overriding getMenuPresenter(), you could also override getPopupPresenter() or getToolbarPresenter(). If you're interested in the latter, see the NetBeans Google Toolbar Module Tutorial.

Join the discussion

Comments ( 5 )
  • Sandip Saturday, July 8, 2006
    Greetjan, as always a cool tip!

    Actually it is better to use BooleanStateAction if you want to implement an action which stores a boolean state. The implementation will use a JCheckBoxMenuItem internally when used on the Menu or Popup Menu. It will use JCheckBox when used on the Toolbar.

    See: http://www.netbeans.org/download/dev/javadoc/org-openide-util/org/openide/util/actions/BooleanStateAction.html

    Read more on how to use BooleanStateAction here http://blogs.sun.com/roller/page/scblog?entry=toggle_action_in_netbeans_menubar



    Secondly the JRadioButtonMenuItem or JRadioButton are meant to be used in cases where it represents an exclusive choice within a set of related choices.

    See: http://java.sun.com/products/jlf/ed2/book/HIG.Controls4.html

  • Geertjan Saturday, July 8, 2006
    How can you start off by saying "as always a good tip" and then proceed to show that everything in the tip is wrong?! :-) Anyway, thanks a lot for the corrections and I will look into them (and probably blog about it soon).
  • Sandip Saturday, July 8, 2006
    I did not say anything about your tip being wrong. I was simply showing an alternative available for implementing boolean state actions. Your tip is a valid approach especially when one wants to use RadioButtons (in conjunction with ButtonGroup).
  • Geertjan Saturday, July 8, 2006
    OK, OK. Plus, you had already blogged about it, so I should have been aware of your approach already and at the very least mentioned it. So, I have no excuses for my ignorance. :-)
  • Paul Nahay Thursday, June 18, 2009

    Anybody know how to change the checkmark or radiomark icons? I don't see any way to do it.

    (Please email me at pnahay@sprynet.com if you do!)

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.