Class ComponentEventBus

java.lang.Object
com.vaadin.flow.component.ComponentEventBus
All Implemented Interfaces:
Serializable

public class ComponentEventBus extends Object implements Serializable
An event bus for Components.

Handles adding and removing event listeners, and firing events of type ComponentEvent.

Events can either be fired manually through fireEvent(ComponentEvent) or automatically based on a DOM event (see DomEvent). Automatically fired events must have a suitable constructor, as defined in DomEvent.

Since:
1.0
Author:
Vaadin Ltd
See Also:
  • Constructor Details

    • ComponentEventBus

      public ComponentEventBus(Component component)
      Creates an event bus for the given component.
      Parameters:
      component - the component which will be used as a source for all fired events
  • Method Details

    • addListener

      public <T extends ComponentEvent<?>> Registration addListener(Class<T> eventType, ComponentEventListener<T> listener)
      Adds a listener for the given event type.
      Type Parameters:
      T - the event type
      Parameters:
      eventType - the event type for which to call the listener
      listener - the listener to call when the event occurs
      Returns:
      an object which can be used to remove the event listener
    • addListener

      public <T extends ComponentEvent<?>> Registration addListener(Class<T> eventType, ComponentEventListener<T> listener, Consumer<DomListenerRegistration> domListenerConsumer)
      Adds a listener for the given event type, and customizes the corresponding DOM event listener with the given consumer. This allows overriding eg. the debounce settings defined in the DomEvent annotation.

      Note that customizing the DOM event listener works only for event types which are annotated with DomEvent. Use addListener(Class, ComponentEventListener) for other listeners, or if you don't need to customize the DOM listener.

      Type Parameters:
      T - the event type
      Parameters:
      eventType - the event type for which to call the listener, must be annotated with DomEvent
      listener - the listener to call when the event occurs
      domListenerConsumer - a consumer to customize the behavior of the DOM event listener, not null
      Returns:
      an object which can be used to remove the event listener
      Throws:
      IllegalArgumentException - if the event type is not annotated with DomEvent
    • hasListener

      public boolean hasListener(Class<? extends ComponentEvent> eventType)
      Checks if there is at least one listener registered for the given event type.
      Parameters:
      eventType - the component event type
      Returns:
      true if at least one listener is registered, false otherwise
    • getListeners

      public Collection<?> getListeners(Class<? extends ComponentEvent> eventType)
      Returns all listeners that match or extend the given event type.
      Parameters:
      eventType - the component event type
      Returns:
      A collection with all registered listeners for a given event type. Empty if no listeners are found.
    • fireEvent

      public void fireEvent(ComponentEvent event)
      Dispatches the event to all listeners registered for the event type.
      Parameters:
      event - the event to fire