March 15, 2002
n "Events and Listeners," I outlined a generic recipe for creating and firing custom events. Since the original answer published, I've received several
emails requesting a complete, concrete example.
In the original Java Q&A, I specified that you need three elements to generate and listen to custom events:
Let's quickly review each.
Note: You can download this article's source code from Resources.
Any class can fit an event's role. At a minimum, an event should provide a method that allows the listener to retrieve the event's source.
Java includes a few built-in super types for creating events. Have a look at:
Normally you extend AWTEvent for events generated by a graphical component and EventObject any other time. If you do use one of these super types, you merely extend it and provide the methods unique to your custom
event.
When creating an event class, you must guarantee that the event is immutable. The event generator will share the same event instance among the listeners; so ensure any one listener cannot change the event's state.
The listener interface provides the contract between the listeners and the event generator. The contract provides the event generator with the method to call when it fires an event.
When creating an event listener interface, you can add as many methods as you need. However, by convention, each method normally takes only one argument: the event.
An event generator tracks listeners, provides a mechanism to add and remove listeners, and, at the appropriate time, fires events to the listeners.
When creating an event generator, make sure its registration mechanism is thread safe. Beyond that, writing an event generator class closely resembles writing any other class.
In this article, I bring back MrHappyObject first seen in "Client Callbacks."

Figure 1. Mr. Happy Object
In the original example, Mr. Happy Object became either happy, annoyed, or angry based on receiving hugs or pinches. In this
example, we'll change how we learn about MrHappyObject's mood. Instead of calling a method, we'll register listeners with MrHappyObject. Whenever he changes his mood, MrHappyObject will send an event to his listeners.
Let's start by defining the new MoodEvent and some Mood constants.
MrHappyObject has three moods: happy, annoyed, and angry. When he fires an event, the listener will retrieve a mood. So, let's first define
some mood constants:
public class Mood {
public static final Mood HAPPY = new Mood( "happy" );
public static final Mood ANNOYED = new Mood( "annoyed" );
public static final Mood ANGRY = new Mood( "angry" );
private String _mood;
public String toString() {
return _mood;
}
private Mood( String mood ) {
_mood = mood;
}
}
While we could have made do with some string constants, this approach is a little cleaner, in my opinion.
That hit spotBy Anonymous on January 25, 2010, 8:05 pmI've been banging my head trying to figure out java events. This posting finally got me there. Thanks.
Reply | Read entire comment
Thank youBy Anonymous on October 24, 2009, 11:13 amMr Happy strikes again - it helped a lot ! Thank you
Reply | Read entire comment
Simple yet complete exampleBy Anonymous on September 30, 2009, 12:47 pmThanks for a great tutorial! I see not many JAVA books explain custom events...helped me understand the concept well...Can you also explain how events can be queued...
Reply | Read entire comment
Thanks.By Anonymous on August 4, 2009, 8:12 amNice tutorial. Really helped me. Thanks!
Reply | Read entire comment
Great tutorialBy Anonymous on June 30, 2009, 6:15 amWow, this is as far the best tutorial i've ever seen on java event. I had hard times understanding how event working in java before, but thanks to you i now understand. Thanks
Reply | Read entire comment
View all comments