JavaBean Events   «Prev  Next»
Lesson 4 Event sources
ObjectiveLearn how event sources are used to facilitate event communication.

JavaBeans Event Sources Communication

In JavaBeans, event sources are components that generate events to communicate changes or actions to other components, known as event listeners. This mechanism enables loosely coupled, event-driven communication, a core feature of the JavaBeans architecture. Here's how event sources facilitate this process:
  • Event Generation: An event source is a JavaBean component (e.g., a button or a text field) that can produce events when specific actions or state changes occur, such as a button being clicked or a property value changing. These events are encapsulated in event objects (e.g., ActionEvent, PropertyChangeEvent), which carry relevant information about the event.
  • Listener Registration: The event source provides public methods to register and unregister event listeners, typically following the naming convention add<EventType>Listener and remove<EventType>Listener (e.g., addActionListener, removeActionListener). These methods allow other components (listeners) to subscribe to specific types of events generated by the source. The source maintains a list of registered listeners, often using a data structure like EventListenerList.
  • Event Notification: When an event occurs, the event source iterates through its list of registered listeners and invokes the appropriate method on each listener, passing the event object as an argument. For example, when a button is clicked, the source calls the actionPerformed(ActionEvent e) method on all registered ActionListener objects. This ensures that all interested components are notified and can respond to the event.
  • Event Handling by Listeners: Listeners, which implement specific event listener interfaces (e.g., ActionListener, PropertyChangeListener), define the logic to handle the event. This decouples the event source from the handling logic, allowing multiple listeners to react to the same event independently.

Example in Context Consider a JButton (a JavaBean component) as an event source:
  • Event Source: The JButton generates an ActionEvent when clicked.
  • Listener Registration: A listener (e.g., a class implementing ActionListener) is registered using button.addActionListener(listener).
  • Event Notification: When the button is clicked, it creates an ActionEvent and calls actionPerformed(ActionEvent e) on all registered listeners.
  • Listener Response: The listener's actionPerformed method executes, performing actions like updating a UI or processing user input.
  • Key Features of Event Sources in JavaBeans
    • Standardized Design Patterns: JavaBeans use a consistent event-handling model based on the Observer pattern, ensuring interoperability across components.
    • Multicast Event Delivery: Event sources support multiple listeners, enabling flexible and extensible communication.
    • Type-Safe Events: Events are strongly typed (e.g., ActionEvent, MouseEvent), ensuring that only appropriate listeners handle specific events.
    • Property Change Events: For bound properties, event sources fire PropertyChangeEvents to notify listeners of state changes, supporting dynamic updates in JavaBeans-based applications.

Practical Use in JavaBeans
Event sources are critical in GUI frameworks like Swing and AWT, where components like buttons, sliders, or text fields act as event sources to enable user interaction. In a broader JavaBeans context, they support modular application design, such as in IDEs or visual builders, where beans communicate state changes or user actions to other components without tight coupling. This event-driven model ensures that JavaBeans components are reusable, interoperable, and capable of integrating into complex systems, making event sources a foundational aspect of JavaBeans communication.

JavaBean's Event Source

An event source[1] is a component capable of generating events. The "event source" must provide a means of registering listeners, which is how the sources know where to send event notifications. When an event occurs within an event source, the source is responsible for examining the registered listeners and sending event notifications appropriately.
  • Java Components that support 1) low-level and 2) semantic event listener interfaces
    Similar to event listeners, event sources can be distinguished by whether they generate low-level or semantic events. Following are the standard Java components that generate low-level events, and therefore support low-level event listener interfaces:
    1. Component
    2. Dialog
    3. Frame

    Following are the standard Java components that support semantic event listener interfaces:
    1. Button
    2. Choice
    3. Checkbox
    4. CheckboxMenuItem
    5. List
    6. MenuItem
    7. Scrollbar
    8. TextField

JavaBeans Event Model

  1. Based the Java 1.1 event model
  2. An object interested in receiving events is an event listener – sometimes called event receiver
  3. An object that generates (fire) events is called an event source – sometimes called event sender
  4. Event listeners register their interest of receiving events to the event source
    Event source provides the methods for event listeners to call for registration
  5. The event source maintains a list of listeners and invoke them when an event occurs

In the next lesson, you learn how to handle events by registering event listeners.
[1] Event source: A component capable of generating events.

SEMrush Software