• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to determine object type.

 
SoonAnn Lim
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am trying to determine the type of the object that generates an action event. In my case, my JMenuItem an JButton register with the same event listener. In actionPerformed method, i need to tell which object generates the event. ActionEvent.getSource() method gives me the object. How can i tell which class(JMenuItem,JButton) they belong to?
Thanks.
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
after you get the object you the getClass() method on it and then the getName() method on that class.
Object myObject=mouseEvent.getSource();
Class myClass=myObject.getClass();
System.out.println(myClass.getName());
 
Jeff Smith
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use instanceof:

Jeff
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!