This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Navigating menu hierarchy in a JPopupMenu Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Navigating menu hierarchy in a JPopupMenu" Watch "Navigating menu hierarchy in a JPopupMenu" New topic
Author

Navigating menu hierarchy in a JPopupMenu

Mike Dahmus
Greenhorn

Joined: Mar 07, 2002
Posts: 29
Hi all,
I'm trying to use one ActionListener for a large popup menu which can be dynamically modified by my customer classes; i.e., I don't necessarily know in advance what will be in there.
I can, however, ensure that I get an actionPerformed on each of those menuitems.
So, in my actionPerformed, I'm trying to figure out how to navigate the "menu hierarchy" and determine my full "menu path"; i.e., if the user selected "Item 3" in "Submenu 2", I want to be able to go from the menuitem whose text is "Item 3" (which I can get from the event) and find its parent, and find its parents' label.
Where I'm stuck right now is determining what the parent's name/label is. I can find the containing submenu for the menuitem; and it's a JPopupMenu (even though when added, it was actually a JMenu); which doesn't seem to have any label or name field which actually functions. It certainly doesn't have the label that I originally gave it ("submenu").
In short:
I get an actionPerformed on a menuitem, and I'd like to be able to generate a "fully qualified" menu name, like "Submenu 2->Menuitem 3", specifically WITHOUT having handles on everything in the menu.
Any help would be appreciated.
Thanks,
Mike Dahmus
mdahmus@io.com


-----Mike Dahmus mike@dahmus.org
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
I haven't checked on this, but setActionCommand(String) migth be worth a try. you would have to set for each item its label (+ any parent action commands/labels) as action command.
chantal
Mike Dahmus
Greenhorn

Joined: Mar 07, 2002
Posts: 29
Originally posted by Chantal Ackermann:
I haven't checked on this, but setActionCommand(String) migth be worth a try. you would have to set for each item its label (+ any parent action commands/labels) as action command.
chantal

Thanks, Chantal! That's exactly what I was looking for and is a great solution to the problem.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Navigating menu hierarchy in a JPopupMenu
 
Similar Threads
Menu
JButtons inside JTooltip
Creating a MenuBar from Xml using Java
Applet -Servlet connection in JNLP
getting the name of a menu item's parent menu