File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes getSource()  in actionPerformed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "getSource()  in actionPerformed" Watch "getSource()  in actionPerformed" New topic

getSource() in actionPerformed

Andy Powell

Joined: Apr 18, 2010
Posts: 20
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

Joined: Jun 13, 2009
Posts: 2451
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

Joined: Apr 18, 2010
Posts: 20
That sorted it. Thank you so much!
I agree. Here's the link:
subject: getSource() in actionPerformed
It's not a secret anymore!