File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JMenu problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JMenu problem" Watch "JMenu problem" New topic
Author

JMenu problem

colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Hi,
I have a JMenu, shapes, and other JMenu's.. AAA,BBB,CCC say.

This is set out like..

So when a JMenuItem is selected, I need to know which of
AAA,BBB,CCC the item belongs to.

I know I can get the object itself using .getParent(),
but I need to get the name, eg "AAA", or its position in the list 0,1,2.

Can anyone help?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
add a separate actionListener to each of the JMenuItems, or
create an ActionListener class and use getSource() to ID which JMenuItem
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Sorry, I don't get it.
I just want to know how to get the name or index in the list,
of the JMenu from which the selected JMenuItem belongs.

Thanks for any help.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
this is just one of the ways

Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Sorry, I misunderstood your question.
Here's a couple more ideas:
colin shuker
Ranch Hand

Joined: Apr 11, 2005
Posts: 744
Thanks very much for both of your helps.
Michaels solution just gives the JMenuItem which I know how to do.
It was the Menu name(as String) that I really wanted.

Craigs solution is excellent, I haven't read it yet, but I just loaded it into eclipse, and it works, its great that you provide great quality thorough code for all my swing needs

Thanks again.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 863
    
    1
Originally posted by colin shuker:
Michaels solution just gives the JMenuItem which I know how to do.
It was the Menu name(as String) that I really wanted.


I don't get it. If you have the JMenuItem then getting the name is as simple as calling its getText() method.

But in your example the name and the actionCommand will be the same, so if you wish you can retrieve the name directly from the ActionEvent: ae.getActionCommand();


bitguru blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JMenu problem