• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JMenu problem

 
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
add a separate actionListener to each of the JMenuItems, or
create an ActionListener class and use getSource() to ID which JMenuItem
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is just one of the ways

 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I misunderstood your question.
Here's a couple more ideas:
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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();
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic