Hi there,

I just wanted to find out if one can have a actionListener() in one .class file and then have the actionEvent() in another .class file? If not, how would I go about to get the same effect?

Thanks!

Recommended Answers

All 7 Replies

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.