Skip to main content

ActionListener for button in a different class

3 replies [Last post]
enividity
Offline
Joined: 2010-04-13

I have two containers in two classes and I'm adding them to a JFrame. Can I have a button in one container that I can add a ActionListener for that the action is in the other container?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kirillcool
Offline
Joined: 2004-11-17

Yes

enividity
Offline
Joined: 2010-04-13

Well sweet :)

I assume its this line of code that needs changing:
button.addActionListener(this);

What do I replace "this" with to point to a actionPerformed method in another class?

kirillcool
Offline
Joined: 2004-11-17

An instance of that class assuming that it implements the ActionListener interface.