• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getSource() in actionPerformed

 
Andy Powell
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having a problem where i've got 2 buttons, I've added actionListeners to both with


and I'm using


to differentiate between the two buttons but neither of them do what they are supposed to.
If I click either then it says "click" like its supposed to but its not telling which is which.

If you get what I mean?

Any help would be greatly appreciated.

Also, if I were to print event.getSource() out to screen should I be getting something as long as this ...


I don't think it makes any difference but that is from the build button

Thanks in advance
 
Rob Camick
Ranch Hand
Posts: 2613
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably because you defined each button twice. Once as a class field and once as a local field. If this is the case the button referenced in the ActionListener will be null. The solution is to not define the local field:



For more help you should create a SSCCE, that demonstrates the incorrect behaviour.
 
Andy Powell
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That sorted it. Thank you so much!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic