How To Create Your Own Events In Java

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:

  1. An Event Class
  2. An Interface
  3. An event source
  4. 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.


14 Responses to “How To Create Your Own Events In Java”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 59 other followers