blob: 33853f9ac02aee686f31618f59021f931565420c [file] [log] [blame]
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
= DevFaqAddIconToContextMenu
:jbake-type: wiki
:jbake-tags: wiki, devfaq, needsreview
:jbake-status: published
:keywords: Apache NetBeans wiki DevFaqAddIconToContextMenu
:description: Apache NetBeans wiki DevFaqAddIconToContextMenu
:toc: left
:toc-title:
:syntax: true
=== 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.
* By using setEnabled, it makes sure every listener will be notified (JButton, JMenuItem...). Moreover it is safer to call it on the EDT since it is not sure which thread triggered the event in the first place.
[source,java]
----
@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 LookupListener lookupListener;
private final Lookup.Result<Project> result;
public HelloIconAction() {
putValue(SMALL_ICON, ImageUtilities.loadImageIcon(ICON, false));
putValue(NAME, Bundle.CTL_HelloIconAction());
putValue("iconBase", ICON);
result = Utilities.actionsGlobalContext().lookupResult(Project.class);
this.lookupListener = new LookupListener() {
@Override
public void resultChanged(LookupEvent ev) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
setEnabled(!result.allInstances().isEmpty());
}
});
}
};
result.addLookupListener(WeakListeners.create(LookupListener.class, this.lookupListener, result));
this.lookupListener.resultChanged(null);
}
@Override
public void actionPerformed(ActionEvent ev) {
for (Project project : result.allInstances()) {
JOptionPane.showMessageDialog(null, "Hello colorful project.\n" + project.toString());
}
}
@Override
public JMenuItem getPopupPresenter() {
return new JMenuItem(this);
}
}
----
[1] link:http://wiki.netbeans.org/DevFaqChangeMenuItemToolbarAppearanceForAction[http://wiki.netbeans.org/DevFaqChangeMenuItemToolbarAppearanceForAction]
[2] link:http://forums.netbeans.org/topic40762.html[http://forums.netbeans.org/topic40762.html]
[3] link:http://benkiew.wordpress.com/2012/12/28/netbeans-how-to-create-a-context-aware-action-with-an-icon-for-the-context-menu/[http://benkiew.wordpress.com/2012/12/28/netbeans-how-to-create-a-context-aware-action-with-an-icon-for-the-context-menu/]
=== Apache Migration Information
The content in this page was kindly donated by Oracle Corp. to the
Apache Software Foundation.
This page was exported from link:http://wiki.netbeans.org/DevFaqAddIconToContextMenu[http://wiki.netbeans.org/DevFaqAddIconToContextMenu] ,
that was last modified by NetBeans user Jmborer
on 2013-10-17T13:27:02Z.
*NOTE:* This document was automatically converted to the AsciiDoc format on 2018-02-07, and needs to be reviewed.