How to write listners in java !!

Hello Ranchers,
I want to implement some custom listeners in my application . for e.g. I want to listen to the event whenever user does some X task. This can be implemented through observer pattern. But I want to have the interface with some custom methods in it like getXChangedEvent() etc and application developer will just implement these interface and listen to the specific events. Can anybody please guide me for the same
Thanks in advance
Samir

If you extend AWTEvent you override paramString instead of toString. An example:

- create an interface that extends java.util.EventListener or one of its sub interfaces:

The name of the methods usually indicate what happened, like actionPerformed, stateChanged, treeNodesInserted.

- if there is more than one method in the listener, create an abstract implementing class that implements all methods with an empty body. By subclassing this class you can avoid having to implement all methods; just override the ones you need:

In the code that will fire the triggers you can use two basic methods.

You should generally use EventListenerList if you have more than one listener type, or your super class already has it. JComponent for instance has a protected instance; you should reuse that.

The fire methods should be named fire followed by the event method name with its first character changed to uppercase. For example, fireActionPerformed, fireStateChanged, etc. The parameters should either be an event object you create elsewhere, or any parameters you need for constructing the event. If you want both, that's just fine. Overloaded versions are not a problem at all.

If you want more examples, I suggest you look in the javax.swing package; the source is available as src.zip in your JDK folder.

Thanks a lot Rob.
That really gave me a deep insight.
I have few doubts though .
1) What difference will it make if I don't extend MyEvent Class from the EventObject class
2) Who will invoke fireFirstEvent, fireSecondEvent. Or will there be a single method called fireEvent which will fire all the events in the list ?
3) If I am thinking correctly , List will have the implementation classes of MyListner .. .rt ?

samir ware wrote:1) What difference will it make if I don't extend MyEvent Class from the EventObject class

You don't get the automatic source field. That's about it. Although it is highly recommended to be a direct or indirect subclass of EventObject, it's no real requirement.

2) Who will invoke fireFirstEvent, fireSecondEvent. Or will there be a single method called fireEvent which will fire all the events in the list ?

The class that knows when the events should be fired. With actionPerformed it's the button on which the user clicked, with PropertyChangeSupport it's mostly the class whose properties have changed.
The general rule is: when something happens inside a class, and you want other classes notified, that is the point where you fire your events.

3) If I am thinking correctly , List will have the implementation classes of MyListner .. .rt ?

If you go for method 1, then the List will indeed contain the implementation classes. You as a supplier shouldn't care what implementations though, as long as they implement the interface.