Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java
Expertise: Advanced
Mar 16, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Implementing the EventListener Interface

The EventListener interface is a marker or tag interface that doesn't define any methods. Its purpose is to provide a base interface for all event listener objects that follow the Listener pattern (see Tip "Understanding the Listener Pattern"). To implement the Listener pattern, you must define the method that needs to be called when the event of interest occurs. This method is known as the event handler. The event producer has a list of event listeners (see Tip "Writing an Event Producer"). As a result, the event producer can call the event handler method on the EventListener. When the event producer is ready to produce the event, it constructs the event object and calls the event handler method on the event listener.

The event producer may not actually construct the event object in case it receives it from some other source. In that case, it will just propagate a reference to the received object. This results in propagation of events from one source to the next.

This class is an example of a simple EventListener. It defines a single method called myEventOccured(). This method prints out the "id" of the event it receives. The MyEvent class is defined in the Tip "Using the EventObject Class."

1.   public class MyEventListener implements EventListener {
2.     public void myEventOccured (MyEvent myEvent) {
3.       System.out.println("received , id = " + myEvent.getID());
4.     }
5.   }
Ajit Sagar
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date