Writing an Event Producer

An event producer is also known as the “source” of the event. Java does not define a base class or an interface for the event producer. However, the Listener design pattern requires that the event source provide at least these two methods for adding and removing listeners for the event that it produces:

 public void add Listener (Listener e)public void removeListener(Listener e)

The angle brackets (<>) represent that the “Event” should be replaced by the actual name of the event. In other words, the name of the methods depends on the event being produced. This naming convention is not enforced by Java’s classes. However, not adhering to the convention will make the user classes unsuitable for use in Java components as the Java Beans model assumes these conventions. Here is a simple example of an event producer:

 1. public class MyEventProducer {2.     private int id_ = 0;3.     Vector MyEventListener myEventListeners_ = new Vector();4. 5.     public void addMyEventListener (MyEventListener listener) {6.       listeners_.add(listener);7.     }8. 9.     public void removeMyEventListener(MyEventListener listener) {10.       listeners_.remove(listener);11.  }12. 13.  public void notifyMyEventOccured() {14.    MyEvent myEvent = new myEvent(this, i++);15.    while myEventListeners_.hasMoreElements() {16.      MyEventListener listener = myEventListeners_.nextElement();17.      listener.myEventOccured(myEvent);18.    }19. }

The MyEventProducer class defines three methods. The first two are used to add and remove listeners to and from a Vector of MyEventListener objects. The MyEventListener class is described in the Tip “Implementing the Event Listener Interface.” The third method creates and fires the event. The MyEvent object is described in the Tip “Using the EventObject Class.”

The event is constructed on Line 14. A reference to “this” is passed as the “event source” parameter and a unique integer is passed as the “id.” In Lines 15-17, the code goes through all the listeners in the list and calls the method myEventOccured() on each listener. The event object is passed as the parameter, which constitutes the “firing” of the event.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may