This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach 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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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: 2152
    
    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!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: getSource() in actionPerformed
 
Similar Threads
New to GUI, need help with actionListener and buttons
Problem with editable combo box!Pls Help me
Global array is null after its been filled
Runtime
applet size