Action listener and interfaces
I understood interfaces until I started using Swing components and event listener classes. My book says that all event listener classes must implement an interface. Why is this?
I believed that interfaces were for the sake of "make sure you add these methods" type of agreement. How is implementing an interface different with event listener classes?
privateclass MyButtonListenerimplements ActionListener
publicvoid actionPerformed(ActionEvent e)
{
code todo stuff
}
}
It's almost like the implement ActionListener does more than make an agreement that it should include the actionPerformed method. Why doesn't it just work without agreeing to an interface?
Sorry about the wordiness, but I can't find a resolution on this anywhere and it's been irking me.
[1103 byte] By [
spysmily1a] at [2007-11-27 5:51:47]

> I understood interfaces until I started using Swing
> components and event listener classes. My book says
> that all event listener classes must implement an
> interface. Why is this?
Why not? Because they are EventListeners without a specific implementation. Don't you think it would make sense?
> I believed that interfaces were for the sake of "make
> sure you add these methods" type of agreement.
Yes. They're also sort of part of the inheritance tree.
> How
> is implementing an interface different with event
> listener classes?
Not at all.
> It's almost like the implement ActionListener does
> more than make an agreement that it should include
> the actionPerformed method. Why doesn't it just work
> without agreeing to an interface?
Huh? Why "more than an agreement"? You're supposed to provide an actionPerformed() method. I don't see what else there is.
An Exception might be marker intrefaces, that don't say "make sure you implement a certain method" but "make sure you implement certain constraints the compiler can't check".
Without an interface, you could still use an abstract superclass of course. But you will have to know either that or the interface, or you won't be able to use it. Consider providing a String instead on an ActionListener. I doubt it'll work.
The Beeper class implements the ActionListener interface, which contains one method: actionPerformed. Since Beeper implements ActionListener, a Beeper object can register as a listener for the action events that buttons fire. Once the Beeper has been registered using the Button addActionListener method, the Beeper's actionPerformed method is called every time the button is clicked.
See http://java.sun.com/docs/books/tutorial/uiswing/events/intro.html
and
http://java.sun.com/docs/books/tutorial/java/IandI/usinginterface.html
java_2006:
Thanks for the links. I kind of understand the reasoning behind making the class implement the actionlistener so it can be registered as a listener.
My next question is what makes this interface able to give any class the ability to do this?
I've read through the source file for Actionlistener and found it extends the Eventlistener interface. It also imports the java.util.Eventlistener package. Why?
Which Eventlistener does it extend? I found 4 different files with this name, unless it has something to do with the import statement I mentioned in the previous question.
Thanks for your patience and time.
Message was edited by:
spysmily1
Message was edited by:
spysmily1
Cecinest:
Thank you for your informative answers. I still have the question of what gives the Actionlistener interface the ability to make a class registerable with an event object. I've looked through the source code and can't find the connection.
Sorry for the lack of understanding on my part and thanks for the patience and time you have put into answering this question. It is greatly appreciated.
Message was edited by:
spysmily1
Message was edited by:
spysmily1