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_PAT H, 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_PAT H, 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:
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.


Read More about [Presenter.Toolbar & Presenter.Menu...