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
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).]
"JavaRanch, where the deer and the Certified play" - David O'Meara