aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How to write listners in java !! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to write listners in java !!" Watch "How to write listners in java !!" New topic
Author

How to write listners in java !!

samir ware
Ranch Hand

Joined: Jul 27, 2005
Posts: 192
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
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Check out how ActionListener, ChangeListener et all work. In short, this is the basis:

- create a subclass of java.util.EventObject or one of its sub classes:

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.

Method 1 - use a List to store the listeners:


Method 2 - use an EventListenerList to store the listeners:

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
samir ware
Ranch Hand

Joined: Jul 27, 2005
Posts: 192
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 ?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

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.
samir ware
Ranch Hand

Joined: Jul 27, 2005
Posts: 192
Thanks a lot Rob...that really helps....thanks a lot once again
 
jQuery in Action, 2nd edition
 
subject: How to write listners in java !!