JavaBean Events   «Prev  Next»
Lesson 5 Registering event listeners
ObjectiveLearn about registering event listeners.

Registering event listeners (JavaBeans)

JavaBeans, a component architecture for the development of reusable software components in Java, adhere to specific conventions including property patterns, method signatures, and event handling mechanisms. Event listeners in JavaBeans are crucial for responding to events generated by user interactions or changes in the property values of the beans.

Event Listener Registration in JavaBeans:

1. Define Event Listener Interface: First, you need to define an event listener interface. This interface should extend `java.util.EventListener` and declare methods that will be called when the event occurs.
public interface CustomEventListener extends java.util.EventListener {
   void customEventOccurred(CustomEvent evt);
}

2. Create an Event Object: You need to create an event object that extends `java.util.EventObject`. This object will be passed to the listener methods when the event occurs.
public class CustomEvent extends java.util.EventObject {
   public CustomEvent(Object source) {
	   super(source);
   }
}

3. Implement Event Generation: Inside your JavaBean, implement the logic to generate events and notify registered listeners. This involves maintaining a list of listeners and providing methods to add and remove listeners.
public class CustomBean {
   protected transient java.util.List<CustomEventListener> listenerList = new java.util.ArrayList<>();

   public synchronized void addCustomEventListener(CustomEventListener listener) {
	   if (!listenerList.contains(listener)) {
		   listenerList.add(listener);
	   }
   }

   public synchronized void removeCustomEventListener(CustomEventListener listener) {
	   listenerList.remove(listener);
   }

   protected void fireCustomEvent() {
	   CustomEvent evt = new CustomEvent(this);
	   for (CustomEventListener listener : listenerList) {
		   listener.customEventOccurred(evt);
	   }
   }
}


4. Implement Event Listener: Create a class that implements the event listener interface and provide implementations for the methods declared in the interface.
public class CustomEventListenerImpl implements CustomEventListener {
   @Override
   public void customEventOccurred(CustomEvent evt) {
	   System.out.println("Event occurred in " + evt.getSource());
   }
}

5. Register and Test the Listener: Finally, create an instance of your JavaBean and an instance of your event listener. Register the event listener with the JavaBean and test the event notification mechanism.
public class Test {
   public static void main(String[] args) {
	   CustomBean bean = new CustomBean();
	   CustomEventListener listener = new CustomEventListenerImpl();

	   bean.addCustomEventListener(listener);
	   bean.fireCustomEvent();  // This should trigger the event and call customEventOccurred method
   }
}
   

JavaBeans utilize a delegation-based event model, allowing objects to delegate their event-handling responsibilities to other objects, known as listeners. This design promotes a clean separation between the source of events and the objects that handle them, facilitating a more modular and maintainable codebase. Event listeners are registered with the event source, and upon the occurrence of an event, all registered listeners are notified, ensuring a robust and responsive application design.

For an application to handle events generated by a Bean, it must register itself as an event listener by calling an event listener registration method on the Bean. For an application to be a valid event listener, it must implement an event listener interface; this involves implementing all of the event response methods[1] defined by the event listener interface. As an example, the following code shows how an application registers itself by calling an event listener registration method:


someBean.addMouseListener(this);

This code, which would appear in the application's constructor, registers the application as a mouse event listener for the someBean Bean.
In the next lesson, you learn how to implement event listeners.
[1]Event response method: a method defined in an event listener that is used to respond to event notifications sent by an event source.