• 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

Help on finding the component

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have some buttons which will popup the same menu on a panel.
How do I get the button where the popup menu is on in the Action class?
The action class is associated with one of the menuitems.

Thanks!
 
Jason Chen
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Help!!
Does anyone know this?
Thanks a lot!!!
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, Jason. We don't ask much of our participants, just that they be nice and that their displayed names consist of a first and last name and not be obviously fake. Please adjust your displayed name to meet the JavaRanch Naming Policy. You can do that here.
As for your question, I think it is a little on the vague side. We're all volunteers here and tend to take the most interesting questions first. The vague and tedious questions tend to get ignored.
I take it you are having trouble telling which button is causing an ActionEvent to occur? You should take a look at the Java Tutorial: Writing a GUI with Swing: Writing Event Listeners. There's plenty of examples there. If you are working with any of the subclasses of EventObject (ActionEvent, MenuEvent, etc.), you can find out what object triggered the event with the getSource() method. Of course, doing the cast back to a useful class and narrowing down which instance is which is tedious. I prefer using the setActionCommand() of java.awt.Button/javax.swing.JButton and getActionCommand() of java.awt.event.ActionEvent to differentate the event directly, rather than the source indirectly. Another popular method to differentiate user actions is to use an anonymous class to invoke a seperate method for each button's ActionEvent.
 
Jason Chen
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you! As you required I changed my display name.

Actually the situation is I have tens of buttons which is created dynamically. when I click a button, it will popup a menu.
My question is how I know on which button I've clicked when I click a menuitem, say "Edit", on the popupmenu.

If I use getSource or getComponent what I get is a instance of menuitem.

Thanks again for your help.
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jason Chen:

My question is how I know on which button I've clicked when I click a menuitem, say "Edit", on the popupmenu.



So your question isn't how to get the component, but how to differentiate between the components you have created. Maybe my second suggestion above is what you are looking for, create an ActionCommand on the fly to identify each instance. You could also create ActionListener instances which have identification info in them. Likewise, JButtons and JMenuItems can be associated with an Action which is invoked when the button is pressed. Your Action implementation could contain information and references to resolve what needs to be done.
[ January 04, 2005: Message edited by: Joe Ess ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic