This question doesn't quite make sense - the actionEvent is created by Swing normally. Can you please explain exactly what you want to achieve?

Cool, okay so say I have one class that's just my UserInterface. Then in that interface I have a actionListener registerd on a button. Now normaly you do that and then you have a method actionEvent/actionPerformed method in the same class that then says what should happen when that button is clicked.

What I want to do, is have this actionEvent/actionPerformed method, in ANOTHER class. Make sense?

Two ways to go:
1. Define the whole ActionListener in the second class but add it in the first
myBotton.addActionListener(new OtherClass.OtherActionListener());
2. Define the ActionListener in the first class in the usual way but in the actionPerformed method just call a method in the second class

I see, I think I'll prefer the first way, in the second way I'm gna end up writing too many method, if I have a method designed only for the purpose of calling another method.
Anyways, thank you so much! This was quite helpfull :-)

Oh yes! Just another quick question. If I were to use the first way now. Should both classes implement actionListener or just the second class?

Just the class(es) that actually have an actionPerformed method.
To keep things tidy and localised I'd consider making an inner class in the second class to be the event handler, but that's just me.

okay perfect, the inner class thing sounds like a good idea. Would look more tidy and organised also like you said. Anyways, thanks again!

This article has been dead for over six months. Start a new discussion instead.