devxlogo

Implementing the EventListener Interface

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.   }
See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist