I’m pretty new to the Java language. A couple of years ago I started looking into it then pursued PHP as my main focus, so I never really bit into the Java scene. I now am able to focus on learning Java and I am learning a lot.
So here is something I have learned:
Creating custom objects with their own custom events.
When I learned how to do this (thanks to this helpful article: http://www.javaworld.com/javaworld/javaqa/2002-03/01-qa-0315-happyevent.html ), I was very excited. I must have looked like a complete idiot when I shouted for joy in the office and then had to explain my trimuph to all who would hear. My poor wife even got to listen to the story. I’ve spared my children.
I’ll give a brief overview of what I learned from that article. It is from 2002, but I found it very helpful, so check it out if you can.
Four things you need:
- An Event Class
- An Interface
- An event source
- An event listener
The event class will extend java.util.EventObject like this:
public class MyEventClass extends java.util.EventObject {
//here's the constructor
public MyEventClass(Object source) {
super(source);
}
}
The interface will look something like this:
public interface MyEventClassListener {
public void handleMyEventClassEvent(EventObject e);
}
The event source will look something like this:
public class MyEventSource {
private List _listeners = new ArrayList();
public synchronized void addEventListener(MyEventClassListener listener) {
_listeners.add(listener);
}
public synchronized void removeEventListener(MyEventClassListener listener) {
_listeners.remove(listener);
}
// call this method whenever you want to notify
//the event listeners of the particular event
private synchronized void fireEvent() {
MyEventClass event = new MyEventClass(this);
Iterator i = _listeners.iterator();
while(i.hasNext()) {
(MyEventClassListener) i.next().handleMyEventClassEvent(event);
}
}
}
The event listener will look something like this:
public class MyEventListener implements MyEventClassListener {
// ... code here
//implement the required method(s) of the interface
public void handleMyEventClassEvent(EventObject e) {
// handle the event any way you see fit
}
}
You’ll need to register the MyEventListener object with the MyEventSource object by call its addEventListener method.
So that’s it in a nutshell; not too bad. I think I may create a more usable and complete example.

June 9th, 2009 at 2:11 am
Thanks for putting it in a nutshell,
Cheers
March 14th, 2010 at 6:05 pm
Hey, i just have to create the interface to deal with my specific event, my event class and i have to advise in one class where the event is going to fire right?
March 15th, 2010 at 9:37 am
Basically the class that is going to publish the event (broadcast it) to the listeners (subscribers) maintains a list of these listeners; these listeners implement a specific interface or abstraction that the publisher knows about; and whenever the publisher wants to notify these subscribers, it does so by iterating through them; for instance if my publisher is a GUI form and I have a logic that runs when the button is pushed, then the class that has the logic will subscribe to that particular button push event. Hope that helps.
May 10th, 2011 at 7:10 am
Nice article!
Though you may want to try out “EventListenerList” in java.awt.AWTEvent
July 12th, 2011 at 10:08 am
Thank you! Helped me a lot!!
July 13th, 2011 at 10:12 am
Not a problem. Happy coding.
October 16th, 2011 at 6:47 pm
Excelente, justo lo que andaba buscando! buen trabajo..
“Excellent, just what I was looking for! good job”
November 2nd, 2011 at 12:25 pm
I love this article! You’ve explained custom events in java in such a simple way. It has helped me a lot!
November 8th, 2011 at 3:06 pm
No problem. Glad you liked it.
November 26th, 2011 at 6:05 pm
Very Well done. Thanks for taking the time to explain it. Very Helpful!!!!
November 28th, 2011 at 3:17 pm
yeah no problem glad it helped
December 23rd, 2011 at 3:28 am
Simple, clean and usefull.
Lots of thanks.
January 11th, 2012 at 6:18 am
I think there’s a mistake in the name in the line 13:
MyEventClass event = new EventClass(this);
Should be
MyEventClass event = new MyEventClass(this);
January 11th, 2012 at 4:12 pm
Thanks. Fixed.