Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

getting the name of a menu item's parent menu  RSS feed

 
christine turner
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! I'm wondering if there's a way to get the name of a menu item's parent item. For example:

Main menu (name: main) [note, this is a popup menu]
first submenu (name: sub1)
second submenu (name: sub2)
first subsubmenu (name: foo)

if the user clicks an item in foo, is there any way to get at the names of "sub2" and "main"?

I thought I had it with menuitem.getParent().getName(), but that returns 'null' even though the parent menu is named. So.. any thoughts?

Thanks in advance!

cct
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getInvoker()
 
christine turner
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for answering!
Yes, I did find that, and it would be perfect. NetBeans doesn't want to admit it exists on my JMenuItem object, however. Is there a workaround for that, or am I simply doing something wrong with the syntax? It would seem from the API that getInvoker is only available for popup menus, not menu items.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


look at what getInvoker() returns
 
christine turner
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, yes, I did have the returned object cast to a JPopupMenu in the original code try, sorry. At any rate, even with the cast, or using Component instead, getInvoker() cannot, apparently be called on a JMenuItem Object, rendering its return type somewhat moot.

Additional possibly relevant info: I'm working with NetBeans 6.8, on a Mac with Java 1.5.something. Does that help?

cct
 
Rob Camick
Ranch Hand
Posts: 2753
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
even with the cast, or using Component instead, getInvoker() cannot, apparently be called on a JMenuItem Object,


That is correct, read the API to see which class the getInvoker() method belongs to.
 
christine turner
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getInvoker is a method of the JPopupMenu class. My problem is, how do I get from the event fired by the menu item, to the popup menu the item belongs to?

I'm sorry for being so dense about this, and I truly do appreciate your efforts. I really am trying to understand!

cct
 
Rob Camick
Ranch Hand
Posts: 2753
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What class is the parent of the JMenuItem?
 
christine turner
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh, CLASS parent!! Good grief, I've been thinking in terms of containment heirarchy. Ok, lemme look at all that again.

Thanks for your patience!

cct
 
christine turner
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmmmmmm. Abstract Button, JComponent, JContainer, Component.. none of these have getInvoker? I think maybe I'm still not understanding this... sorry!

cct
 
Rob Camick
Ranch Hand
Posts: 2753
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh, CLASS parent!! Good grief, I've been thinking in terms of containment heirarchy.


Containment hiearchy is the correct thinking.

The JMenuItem is added to some component in order for it to be displayed. So what component is returned when you invoke getParent()? If I remember correctly its a JPopupMenu? Then you can use the getInvoker() method.
 
christine turner
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OH! Gotcha. Yes, the parent is a JPopupMenu, and yes, getInvoker works... so..


But... while it definitely does work, I'm still a bit unclear on /how/ it works. Why doesn't fromParent.getInvoker() return the main menu? 'fromParent' is the parent of the menu item, and it is 'menu 2'. Which is what fromParent.getInvoker() returns. So, getInvoker returns the same object?? If not, what the heck is eventFrom.getParent() returning???

 
Rob Camick
Ranch Hand
Posts: 2753
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have a JMenu called "Main". When you click on it a JPopupMenu is created containing the sub menus "sub1", "sub2" and "foo". When you click on "foo" another JPopupMenu is created containing your JMenuItems.

If you really want the "main" menu then you need looping code to climb the tree hierachy. Some like this:

http://stackoverflow.com/questions/1353610/jmenuitem-getrootpane-workaround
 
christine turner
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I seem to have phrased my last question very badly. The code I listed does work, that is, it does what I was trying to do. Looking at it with a bit more sleep behind me, I guess my problem is that I don't understand the container heirarchy/getParent/getInvoker chain. Would a menu's invoker not be its parent? IE, I would think (apparently incorrectly) that getParent and getInvoker would return the same object. Is that not so?

I really, really appreciate your help with this. You must think I'm the densest thing since plutonium.

At any rate, given that the getParent/getInvoker works, I can now move forward, so THANK YOU! Understanding exactly what's going on would be a nice bonus though.

Again, thanks!!

cct
 
Rob Camick
Ranch Hand
Posts: 2753
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess my problem is that I don't understand the container heirarchy/getParent/getInvoker chain. Would a menu's invoker not be its parent?


Well, I guess the problem is that you have two different conceptsto consider:

1) Normally you add components to a panel and therefore you have a parent / child relationship, so given a component you can get its parent.

2) Menus are different. You don't "add" a JPopupMenu to a JMenu. You add menu items to the menu. It turn a JPopupMenu is "created and displayed" when you click on the menu. So you don't really have a parent/child relationship in the same way as you do above. Instead the term "invoker" is used to get the menu that displayed the popup menu.

Maybe the section from the Swing tutorial on The Layered Pane will help explain in a little more detail.
 
christine turner
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah! Yes, that's what was tripping me up.

Thank you so much for all your help with this! And your patience!

cct


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!