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!

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 08:24 AM PDT #

Good day!

I know this is an old post. I want to thanks for it. I've already check for another solution but this is the best I can find.

Oh! Just one question: What if I want to add one shorcut like Ctrl + Something?

Thanks again!.

Posted by Carlos Corona on August 20, 2010 at 02:53 AM PDT #

Nice tutorial, the only problem is that I cannot see any of the pictures so I find it difficult to follow.

Posted by guest on June 25, 2013 at 12:48 AM PDT #

Thanks, fixed.

Posted by Geertjan on June 25, 2013 at 01:00 AM PDT #

OK, thank you very much

Posted by guest on June 25, 2013 at 01:08 AM PDT #

Hello Geertjan, good evening. I have a question relating to actions. Say for instance I have an action instance in the toolbar eg.(cut and paste). Whenever a topcomponent is opened, I wanted to be able to edit the content of the topcomponent. I'm supposing that I might have to use the lookup api for this, But I'm not sure. I folloed this tutorial to crreate the toolbar buttons(https://platform.netbeans.org/tutorials/71/nbm-google.html).

I just wanted to be able to select a button on the toolbar, then changes would be made to the current topcomponent instance. Any guidance on where to look for solution? I hope my question is understandable.

Posted by Winnifred777 on July 01, 2013 at 09:19 PM PDT #

Hey, I got the solution to my problem following this tutorial you did in 2011. Thanks for sharing: https://blogs.oracle.com/geertjan/entry/how_would_you_greet_the

Posted by guest on July 05, 2013 at 01:29 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today