Download NetBeans!

20090814 Friday August 14, 2009

JRadioButtonMenuItem & JCheckBoxMenuItem on the NetBeans Platform

Creating a toggle button on the NetBeans Platform can be done in different ways, here's one of them. We'll create two actions that let us toggle between two JRadioButtonMenuItems:

We'll begin by creating a utility class that returns a javax.swing.ButtonGroup, to which we'll add the current javax.swing.JRadioButtonMenuItem that we return from the NetBeans Platform Presenter.Menu. All along, we'll be using an ActionListener to define our action, since we're using NetBeans Platform 6.7 or above.

  1. Create a helper class that returns a ButtonGroup:
    import javax.swing.ButtonGroup;
    
    public class ButtonGroupHelper {
        
        public static ButtonGroup bg = new ButtonGroup();
        
        /** Creates a new instance of Group */
        public ButtonGroupHelper() {
        }
        
        /** Returns a ButtonGroup */
        public static ButtonGroup returnGroup() {
            return bg;
        }
        
    }

  2. Create an action for Marge:
    public final class MargeAction implements Presenter.Menu, ActionListener {
    
        private ImageIcon ICON = new ImageIcon(ImageUtilities.loadImage("org/myorg/googletoolbar/marge.png", true));
    
        @Override
        public void actionPerformed(ActionEvent e) {
            // nothing needs to happen here
        }
    
        @Override
        public JMenuItem getMenuPresenter() {
            JRadioButtonMenuItem abc = new JRadioButtonMenuItem("Marge", null);
            ButtonGroup local = ButtonGroupHelper.returnGroup();
            local.add(abc);
            abc.setSelected(false);
            abc.setIcon(ICON);
            abc.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    StatusDisplayer.getDefault().setStatusText("Marge chosen");
                }
            });
            return abc;
        }
    
    }

  3. Create an action for Homer:
    public final class HomerAction implements Presenter.Menu, ActionListener {
    
        private ImageIcon ICON = new ImageIcon(ImageUtilities.loadImage("org/myorg/googletoolbar/homer.png", true));
    
        @Override
        public void actionPerformed(ActionEvent e) {
            // nothing needs to happen here
        }
    
        @Override
        public JMenuItem getMenuPresenter() {
            JRadioButtonMenuItem abc = new JRadioButtonMenuItem("Homer", null);
            ButtonGroup local = ButtonGroupHelper.returnGroup();
            local.add(abc);
            abc.setSelected(false);
            abc.setIcon(ICON);
            abc.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    StatusDisplayer.getDefault().setStatusText("Homer chosen");
                }
            });
            return abc;
        }
    
    }

  4. Make very sure that you've registered the above actions correctly:
    <folder name="Actions">
        <folder name="File">
            <file name="org-myorg-googletoolbar-MargeAction.instance">
                <attr name="delegate" newvalue="org.myorg.googletoolbar.MargeAction"/>
            </file>
            <file name="org-myorg-googletoolbar-HomerAction.instance">
                <attr name="delegate" newvalue="org.myorg.googletoolbar.HomerAction"/>
            </file>
        </folder>
    </folder>
    <folder name="Menu">
        <folder name="File">
            <file name="org-myorg-googletoolbar-MargeAction.shadow">
                <attr name="originalFile" stringvalue="Actions/File/org-myorg-googletoolbar-MargeAction.instance"/>
                <attr name="position" intvalue="0"/>
            </file>
            <file name="org-myorg-googletoolbar-HomerAction.shadow">
                <attr name="originalFile" stringvalue="Actions/File/org-myorg-googletoolbar-HomerAction.instance"/>
                <attr name="position" intvalue="10"/>
            </file>
        </folder>
    </folder>

    As pointed out in the NetBeans Plugin Quick Start, if you're using the New Action wizard to create your actions, you will (at the very least) need to delete the "instanceCreate" attribute because you do not want to create an instance of "org.openide.awt.Actions.alwaysEnabled", in this case. None of the other attributes are relevant in this scenario.

  5. Let's now change the radiobutton menu items to checkbox menu items:

    Firstly, simply change the declaration of the JRadioButtonMenuItem to JCheckBoxMenuItem, in both the action classes:

    JCheckBoxMenuItem abc = new JCheckBoxMenuItem("Homer", null);

    Also, add the following line after the call to "local.add(abc)":

    abc.setState(true);

  6. Finally, we'll present those checkbox menu items as toolbar buttons instead:

    To achieve the above effect, implement Presenter.Toolbar instead of Presenter.Menu in each action. Then change the implementing method to getToolbarPresenter() instead of getMenuPresenter() in each action. Finally, change the folder named "Menu" in the layer to "Toolbars" so that the actions are registered as toolbar buttons instead of menu items. That's all! Now you'll have toolbar buttons instead of menu items.

Now go back and look at how few NetBeans Platform classes you've used. Very few, right? Apart from Presenter.*, and the ImageUtilities class from the Utilities API, everything is standard JDK code. And that's going to increase even further in the coming releases of the NetBeans Platform. Pretty cool!

Aug 14 2009, 01:46:20 AM PDT Permalink

Trackback URL: http://blogs.sun.com/geertjan/entry/jradiobuttonmenuitem_jcheckboxmenuitem_on_the_netbeans
Comments:

Another great tutorial, but I wonder now how to implement this JRadioButtonMenuItem & JCheckBoxMenuItem functionality into Visual Library again to replace MoveControlPointAction (Ctrl key actions). Do you have any hacks for that ? ;)

Posted by Martin Has on August 14, 2009 at 03:24 PM PDT #

Post a Comment:

Name:
E-Mail:
URL:

Your Comment:

HTML Syntax: NOT allowed