This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
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
Author

getSource() in actionPerformed

Andy Powell
Greenhorn

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: 2191
    
    7
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

Joined: Apr 18, 2010
Posts: 20
That sorted it. Thank you so much!
 
GeeCON Prague 2014
 
subject: getSource() in actionPerformed