Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes ActionListeners & Classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "ActionListeners & Classes" Watch "ActionListeners & Classes" New topic
Author

ActionListeners & Classes

T Walsh
Greenhorn

Joined: Nov 22, 2012
Posts: 9
Hello all, I have a question about ActionListeners, my application class entends from JFrame and implements ActionListener so it has a actionPerformed method within that class.

So can i get access to that actionPerformed method in a different class? and if so how?

Kind Regards

Tim
Joel Christophel
Ranch Hand

Joined: Apr 20, 2011
Posts: 225
    
    1

From another class, you would call a component's addActionListener() method and pass in an instance of your JFrame/ActionListener class. For example:



So whenever 'button' is clicked, the actionPerformed() method from your JFrame/ActionListener class will execute.
Wayan Saryada
Ranch Hand

Joined: Feb 05, 2004
Posts: 104

Isn't it more light weight to implements the ActionListener in other separate class instead of in JFrame? Where later on it can be use by the JFrame and other classes that reuse this ActionListener.


Website: Learn Java by Examples
Joel Christophel
Ranch Hand

Joined: Apr 20, 2011
Posts: 225
    
    1

I Wayan Saryada wrote:Isn't it more light weight to implements the ActionListener in other separate class instead of in JFrame? Where later on it can be use by the JFrame and other classes that reuse this ActionListener.


That's generally the way to go. Who knows, though, maybe the OP is doing what he's doing for a specific reason.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36514
    
  16
I shall move this discussion to our GUIs forum.

There is no difference in weight between having Listeners in different sorts of classes. No design is heavyweight or lightweight. There are object‑oriented and non‑object‑oriented versions however. I have a little rule of thumb about Listeners, and where you put their classes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ActionListeners & Classes
 
Similar Threads
Apply 2d array of answer to the right position of JTextField grid with JCheckBox
accessing variable of an outer class from an inner class
Collaboration between separate parts(classes) in the same framework
Netbeans ActionListener
calling another GUI class