• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

how to get menu item source name?

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I created a menu with : Tools
menuitem - Add
subMenuItem - A
subMenuItem - B
menuItem - Update
subMenuItem - A
subMenuITem - B
I also registered the each menuitem with an actionListener, my question is how to get the object source name?
I try the following syntax inside the actionlistener class
JMenuItem itemSource = (JMenuItem) (e.getSource() )
System.out.println("you click: " + itemSource.getText());
return: "A" if i click A.
I actually want to get the object name instead of the source text. So i try, getActionCommand() instead of getSource(), it return to me
this: "javax.swing.JMenuItem[,1,3,101x21,alignmentX=null,alignmentY=null,border=javax.swing.plaf.metal.MetalBorders$MenuItemBorder@25fefa,flags=1056,maximumSize=,minimumSize=,preferredSi ze=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=2,bottom=2,right=2],paintBorder=true,paintFocus=false,pressedIcon=,rollover Enabled=false,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=Application]"
So, i want to know if there is anyway to get the object source name ("the name you define it, e.g JMenuItem menuAddA = new JMenuItem("A"), i want to get menuAddA returns)
Waiting for your reply!
Thank you very much!
Mindy
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An object does not "have a name". A variable has a name, and a variable has a reference to an object.
So you are asking how to get the name of the variable that you defined to hold a reference to that JMenuItem (I think).
Of course the getSource() method returns a reference to the object itself, not any of the multiple variables that might be referencing it. However, once you know the object that sent the event, you can compare to see if it is the SAME object that is referenced in one of your variables.

Which addresses your question, but leaves the issue of WHY do you want to do this? If you have a separate listener for each JMenuItem, you should not NEED to do such a compare. Or is there some OTHER reason?

[This message has been edited by Cindy Glass (edited August 30, 2001).]
 
Mindy Wu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Cindy! I am going to create different actionlistener for each set of menu item.
By the way, can i add a JButton on the menubar?
 
Live ordinary life in an extraordinary way. Details embedded in this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic