• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to write listners in java !!

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
samir ware
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Rob...that really helps....thanks a lot once again
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic