wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Problem with ActionListener and JButtons (harder than it first seems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Problem with ActionListener and JButtons (harder than it first seems" Watch "Problem with ActionListener and JButtons (harder than it first seems" New topic
Author

Problem with ActionListener and JButtons (harder than it first seems

Thomas FairBanks
Greenhorn

Joined: Oct 31, 2008
Posts: 2
This program copies the iPhone sort of:

My problem (lines in bold) is that the buttons that I add the ActionListener to are not in the same class as actionPerformed. When I tried to make the JButtons public (outside of the current class), it gave me some exceptions. What can I do to properly add this ActionListener?
[ October 31, 2008: Message edited by: Thomas FairBanks ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
The best solution for this problem is to use a different ActionListener per Button - then there is no need to call e.getSource at all.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
And welcome to JavaRanch

Thank you, Ilja; you actually had the patience to read all that code!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Campbell Ritchie:

Thank you, Ilja; you actually had the patience to read all that code!


Didn't. Just read the bold lines.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
Originally posted by Ilja Preuss:
Just read the bold lines.


In which case, thank you Thomas FairBanks, for marking the location of the problem.
Thomas FairBanks
Greenhorn

Joined: Oct 31, 2008
Posts: 2
Wow, can't believe I didn't think of that. Thanks Ilja
 
 
subject: Problem with ActionListener and JButtons (harder than it first seems