• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to get the object name in actionPerformed method  RSS feed

 
Chidanand Chauhan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All
I am facing one problem. The problem is
Suppose i have a button in my class named Button1
eg:
JButton b1 = new JButton("Click");
I want to get the object name of the button i.e " b1 ". in actionPerformed method.
how would i get the object name b1.

Please help me.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you add an actionListener to multiple components, you need to compare
the source against the various components, in order to proceed to the necessary action



you can add the actionListener/actionPerformed directly to the button
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As an aside, b1 is not the name of an object, but of a reference variable. An object can be referenced by an arbitrary number of reference variables at the same time, and doesn't have a name by itself.
 
Chidanand Chauhan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THANKS ALOT IT NOW WORKS.
THANKS ONCE AGAIN
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!