Geertjan's Blog

  • December 17, 2012

Dynamically Changing the Display Names of Menus and Popups

Geertjan Wielenga
Product Manager

Very interesting thing and handy to know when needed is the fact that "menuText" and "popupText" (from org.openide.awt.ActionRegistration) can be changed dynamically, via "putValue" as shown below for "popupText". The Action class, in this case, needs to be eager, hence you won't receive the object of interest via the constructor, but you can easily use the global Lookup for that purpose instead, as also shown below.

import java.awt.event.ActionEvent;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import javax.swing.AbstractAction;
import org.netbeans.api.project.Project;
import org.netbeans.api.project.ProjectUtils;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionRegistration;
import org.openide.awt.StatusDisplayer;
import org.openide.util.Lookup;
import org.openide.util.LookupEvent;
import org.openide.util.LookupListener;
import org.openide.util.Utilities;
import org.openide.util.WeakListeners;
category = "Demo",
id = "org.ptt.TrackProjectSelectionAction")
lazy = false,
displayName = "NOT-USED")
path = "Projects/Actions",
position = 0)
public final class TrackProjectSelectionAction extends AbstractAction
implements LookupListener {
private Lookup.Result<Project> projects;
private Project context;
public TrackProjectSelectionAction() {
projects = Utilities.actionsGlobalContext().lookupResult(Project.class);
WeakListeners.create(LookupListener.class, this, projects));
resultChanged(new LookupEvent(projects));
public void resultChanged(LookupEvent le) {
Collection<? extends Project> p = projects.allInstances();
if (p.size() == 1) {
Project currentProject = p.iterator().next();
String currentProjectName =
putValue("popupText", "Current Project: " + currentProjectName);
"Current Project: " + currentProjectName);
context = currentProject;
public void actionPerformed(ActionEvent e) {
protected void refresh() {
DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
String formatted = formatter.format(System.currentTimeMillis());
putValue("popupText", "Last Click At: " + formatted +
" (" + ProjectUtils.getInformation(context) + ")");

Join the discussion

Comments ( 3 )
  • Djamel TORCHE Tuesday, March 5, 2013

    Hi; Seems great ;)

    What about hiding/displaying dynamically the menu Items ? for e.g hide or display some menu items based on the user profile (user, admin) by code at start up.


  • Geertjan Tuesday, March 5, 2013
  • Djamel TORCHE Sunday, March 17, 2013

    Thank you so much, it's exactly what I'm looking for.

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.