DevFaqAddIconToContextMenu

(Difference between revisions)
(How do i add an icon to the context menu?)
Line 27: Line 27:
     private static final long serialVersionUID = 1L;
     private static final long serialVersionUID = 1L;
     private final Lookup context;
     private final Lookup context;
 +
    private final LookupListener lookupListener;
 +
    private final Lookup.Result<Project> result;
     public HelloIconAction() {
     public HelloIconAction() {
Line 33: Line 35:
         putValue(NAME, Bundle.CTL_HelloIconAction());
         putValue(NAME, Bundle.CTL_HelloIconAction());
         putValue("iconBase", ICON);
         putValue("iconBase", ICON);
-
    }
+
        result = contextLookup.lookupResult(Project.class);
 +
        this.lookupListener = new LookupListener() {
 +
 
 +
            @Override
 +
            public void resultChanged(LookupEvent ev) {
 +
                setEnabled(null != contextLookup.lookup(Project.class));
 +
            }
 +
        };
 +
        result.addLookupListener(this.lookupListener);
 +
        this.lookupListener.resultChanged(null);
-
    @Override
 
-
    public boolean isEnabled() {
 
-
        return null != context.lookup(Project.class);
 
     }
     }

Revision as of 13:18, 9 October 2013

How do i add an icon to the context menu?

NOTE: This is not recommended for NB RCP (it will break HMI standards on Mac OS X for example), but is still possible.

Steps:

  • let your action implement Presenter.Popup and return a JMenuItem instance for the current action (see [1], [2])
  • set the icon via putValue(javax.swing.Action.SMALL_ICON, ...)
  • set the icon via putValue("iconBase", ...) to make it work properly in the toolbar. Otherwise you will get a fixed size icon.
@ActionID(
        category = "Build",
        id = "sample.contextmenu.HelloIconAction")
@ActionReferences({
    @ActionReference(path = "Menu/File", position = 0),
    @ActionReference(path = "Loaders/Languages/Actions", position = 0),
    @ActionReference(path="Projects/Actions")
})
@ActionRegistration(
        displayName = "#CTL_HelloIconAction")

@Messages("CTL_HelloIconAction=Hello Icon Action")
public final class HelloIconAction extends AbstractAction implements Presenter.Popup {

    @StaticResource
    private static final String ICON = "sample/contextmenu/sample.gif";
    private static final long serialVersionUID = 1L;
    private final Lookup context;
    private final LookupListener lookupListener;
    private final Lookup.Result<Project> result;

    public HelloIconAction() {
        context = Utilities.actionsGlobalContext();
        putValue(SMALL_ICON, ImageUtilities.loadImageIcon(ICON, false));
        putValue(NAME, Bundle.CTL_HelloIconAction());
        putValue("iconBase", ICON);
        result = contextLookup.lookupResult(Project.class);
        this.lookupListener = new LookupListener() {

            @Override
            public void resultChanged(LookupEvent ev) {
                setEnabled(null != contextLookup.lookup(Project.class));
            }
        };
        result.addLookupListener(this.lookupListener);
        this.lookupListener.resultChanged(null);

    }

    @Override
    public void actionPerformed(ActionEvent ev) {
        JOptionPane.showMessageDialog(null, "Hello colorful project.\n" + context.lookup(Project.class).toString());
    }

    @Override
    public JMenuItem getPopupPresenter() {
        return new JMenuItem(this);
    }
}

[1] http://wiki.netbeans.org/DevFaqChangeMenuItemToolbarAppearanceForAction
[2] http://forums.netbeans.org/topic40762.html
[3] http://benkiew.wordpress.com/2012/12/28/netbeans-how-to-create-a-context-aware-action-with-an-icon-for-the-context-menu/

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo