aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Button Listener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Button Listener" Watch "Button Listener" New topic
Author

Button Listener

Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
I'm stumped on this one, no matter which button I press the getText() method always returns "Delete". I don't see where I'm repeating anything that would cause the same text for all three buttons.

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2093
    
    7
The button variable is a class variable of MenuFrame class, not the ButtonAction class (which is what you want).

Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
I thought I had ButtonAction as an inner class of MainFrame, and that Button was defined as part of the MainFrame class, so it would be accessible to both. No?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2093
    
    7
Yes, but there is only one instance of JButton so it retains the value of the last ButtonListener created.

You need the button as part of the ButtonListener you can access the button that was used to create the listener.
Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
Rob Camick wrote:Yes, but there is only one instance of JButton so it retains the value of the last ButtonListener created.

You need the button as part of the ButtonListener you can access the button that was used to create the listener.



I'm sorry, I'm being a little thick, but that last sentence just doesn't make sense to me.
Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
Ok, I modeled it after this code which I wrote earlier, this works. It seems like the same code.

Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
OK, I figured out what you meant, just took a little while. Thanks.
 
 
subject: Button Listener
 
Similar Threads
Code review
ActionListener and Event
How do I pass a frame into another class?
Errors in GUI program
Image lost behind buttons