X

Geertjan's Blog

  • April 6, 2010

Presenter.Toolbar & Presenter.Menu (Part 1)

Geertjan Wielenga
Product Manager
Let's create a slightly more complex menu item and toolbar button in a NetBeans Platform application. They'll look like this:

Here's the definition of this action:

public final class AddAction implements ActionListener, Presenter.Toolbar, Presenter.Menu {//The action that will be invoked is defined in the layer.xml,
//as the first action within "Actions/MyActions":

Action newTaskAction = Utilities.actionsForPath("Actions/MyActions").get(0);//Icon will be reused, so set its path once:
private String ICON_PATH = "org/netbeansrcp/taskactions/Add.png";
@Override
public void actionPerformed(ActionEvent e) {//Nothing needs to happen here.
}//Define what will be displayed in the menubar:
@Override
public JMenuItem getMenuPresenter() {//First, a JMenu will be displayed...
JMenu menu = new JMenu(NbBundle.getMessage(AddAction.class, "CTL_AddAction"));//...with the icon defined above:
menu.setIcon(ImageUtilities.loadImageIcon(ICON_PATH, true));//The menu item will be the action identified above:
JMenuItem item = new JMenuItem(newTaskAction);
menu.add(item);
return menu;
}//Define what will be displayed in the toolbar:
@Override
public Component getToolbarPresenter() {
return new MyToolbarPresenter();
}
private class MyToolbarPresenter extends JButton {
private JPopupMenu popup;
MyToolbarPresenter() {//Set the text of the button:
this.setText(NbBundle.getMessage(AddAction.class, "CTL_AddAction"));//Set the icon of the button:
this.setIcon(ImageUtilities.loadImageIcon(ICON_PATH, true));//Create a popup:
this.popup = new JPopupMenu();//Add the action from the layer to the popup:
this.popup.add(newTaskAction);//Show the button when the popup is clicked:
this.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
MyToolbarPresenter.this.popup.show(
MyToolbarPresenter.this, 0,
MyToolbarPresenter.this.getHeight());
}
});
}
}
}

It is registered in the layer like this:

<folder name="Actions">
<folder name="Edit">
<file name="org-netbeansrcp-taskactions-AddAction.instance"/>
</folder>
</folder>
<folder name="Menu">
<folder name="Edit">
<file name="org-netbeansrcp-taskactions-AddAction.shadow">
<attr name="originalFile" stringvalue="Actions/Edit/org-netbeansrcp-taskactions-AddAction.instance"/>
<attr name="position" intvalue="0"/>
</file>
</folder>
</folder>
<folder name="Toolbars">
<folder name="Edit">
<file name="org-netbeansrcp-taskactions-AddAction.shadow">
<attr name="originalFile" stringvalue="Actions/Edit/org-netbeansrcp-taskactions-AddAction.instance"/>
</file>
</folder>
</folder>

It's part of Juergen Petri's NetBeans RCP book, but now updated to use ActionListener instead of CallableSystemAction. And SystemAction calls are replaced by Utilities.actionsForPath.

Join the discussion

Comments ( 8 )
  • Rocco Tuesday, April 6, 2010

    Hi Geertjan,

    this is a very interesting post.

    However, I'm wondering: what is wrong in creating submenu by enclosing AddAction.shadow in one more folder, like this:

    <folder name="Menu">

    <folder name="Edit">

    \*\*\*\*<folder name="AddTask">\*\*\*\*

    [...]

    \*\*\*\*</folder>\*\*\*\*

    </folder>

    </folder>

    I know that this does not work with toolbar, but I have used this way to create submenu, so far...

    There is a reason for which should be used in place of the other way?

    Thank you in advance for your reply.


  • Geertjan Wielenga Tuesday, April 6, 2010

    Hi Rocco. I tried but failed to do it that way. Can you give me a working module or a sample or maybe a blog entry that shows a real set of steps I can follow to do it that way (maybe write it on netbeans.dzone.com)? Gj


  • Rocco Wednesday, April 7, 2010

    Hi Geertjan,

    you can get more details here:

    http://netbeans.dzone.com/tips/way-i-create-submenus


  • Geertjan Wielenga Wednesday, April 7, 2010

    Hi Rocco -- you need to add a photo of yourself to your user profile on NetBeans Zone. Then it will be published.


  • Rocco Thursday, April 8, 2010

    Geertjan, thanks for the tip! RC


  • Geertjan Thursday, April 8, 2010

    Here it is, with a nicer URL:

    http://netbeans.dzone.com/how-to-create-nb-submenus

    Maybe you could contact me via e-mail (geertjan dot wielenga at sun dot com) and we can work on an article/interview about how you are using the NetBeans Platform?


  • Yosi Pramajaya Wednesday, January 4, 2012

    Hi Geertjan, I'm using NetBeans 7.1, about this Presenter.Popup, how we can create our own custom action that sensitive with the selected Node? So, when we right-click the node, the pop up is shown, and when we click it, a TopComponent is shown.

    Thanks for your help


  • Geertjan Wednesday, January 4, 2012
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.