NetBeans: How to create a context aware action with an icon for the context menu
Here a small sample how to create a context aware action with icons for a NetBeans Platform RCP application or even the NB IDE. You may wonder and questions like “Why the heck he is doing this” or “Why he won’t use the standard action wizard?” may pop up.
Assigning icons to actions is easy. BUT the icons do will not be displayed in the context menu (by default). It is possible but not best practice in NB RCP. Keep in mind that icons in menus are not supported for MacOS. So use it wisely.
The important points are:
- extend from
AbstractActionto get access toputValue(you loose the standard context aware features, so you have to implement them yourself via keeping a proxy to the global lookup and enabling the action depending on the content of the lookup) - implement
Presenter.Popupand return aJMenuIteminstance for the current action (see [1], [2]) - set the icon via
putValue(javax.swing.Action.SMALL_ICON, ...)
package de.markiewb.netbeans.sample.contextmenu;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import static javax.swing.Action.SMALL_ICON;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import org.netbeans.api.annotations.common.StaticResource;
import org.netbeans.api.project.Project;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionRegistration;
import org.openide.util.ImageUtilities;
import org.openide.util.Lookup;
import org.openide.util.NbBundle.Messages;
import org.openide.util.Utilities;
import org.openide.util.actions.Presenter;
@ActionID(
category = "Build",
id = "de.markiewb.netbeans.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 = "de/markiewb/netbeans/sample/contextmenu/sample.gif";
private static final long serialVersionUID = 1L;
private final Lookup context;
public HelloIconAction() {
context = Utilities.actionsGlobalContext();
putValue(SMALL_ICON, ImageUtilities.loadImageIcon(ICON, false));
putValue(NAME, Bundle.CTL_HelloIconAction());
}
@Override
public boolean isEnabled() {
return null != context.lookup(Project.class);
}
@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://wiki.netbeans.org/DevFaqAddIconToContextMenu
The result:
![]()
See sample code at https://github.com/markiewb/nb-api-samples/tree/master/ContextMenuWithIcon
Leave a Reply Cancel reply
Recent Posts
- NetBeans platform development with JRebel even more simplified
- NetBeans IDE: Additional hints plugin to simplify the work with String literals
- NetBeans IDE: Improved documents tabs in nightly builds
- NetBeans IDE: Custom hints
- NetBeans IDE: Verification of plugins and update center URLs for non-verified plugins
Twitter Updates
- RT @muellermi: Get involved into @NetBeans. Join the NetCAT program. More: blog.mueller-bruehl.de/netbeans/netca… 15 hours ago
- "@antonarhipov: Learn #Git Branching buff.ly/1654L12" visualized with graphs 4 days ago
- RT "@toomasr: #microsoft fetching every URL you post on #skype h-online.com/security/news/… just verified this with a simple honeypot #scary" #wtf 1 week ago
- RT @toomasr: Giving back to the community: 3 ways to keep #jenkinsci growing, jenkins-ci.org/content/giving… 1 week ago
- RT @GeertjanW: Develop mobile apps live on Android with NetBeans IDE! blogs.oracle.com/netbeanswebcli… 1 week ago
Tags
Archives
- May 2013 (1)
- March 2013 (2)
- February 2013 (2)
- January 2013 (4)
- December 2012 (3)
- November 2012 (2)
- October 2012 (1)
- September 2012 (2)
- August 2012 (2)
- May 2012 (4)
- April 2012 (2)
- March 2012 (1)
- February 2012 (2)
- January 2012 (1)
- December 2011 (1)
- July 2011 (2)
- June 2011 (1)
- May 2011 (3)
- February 2011 (1)
- November 2010 (1)
- September 2010 (4)
- August 2010 (2)
- June 2010 (2)
- May 2010 (1)
- March 2010 (3)
- January 2010 (2)
- December 2009 (3)
- November 2009 (3)
Blogroll
Blog Stats
- 24,367 hits
Recent Comments