Class ComponentEvent<T extends Component>

java.lang.Object
java.util.EventObject
com.vaadin.flow.component.ComponentEvent<T>
Type Parameters:
T - the event source type
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
AbstractAttachDetachEvent, AbstractField.ComponentValueChangeEvent, BlurNotifier.BlurEvent, ClickEvent, CompositionEvent, FocusNotifier.FocusEvent, InputEvent, KeyboardEvent, PollEvent, UI.BrowserLeaveNavigationEvent, UI.BrowserNavigateEvent, UI.BrowserRefreshEvent, UploadCompleteEvent, UploadStartEvent, WebComponentUI.WebComponentConnectEvent

public class ComponentEvent<T extends Component> extends EventObject
An event whose source is a Component.

Typically used together with ComponentEventBus.

Since:
1.0
Author:
Vaadin Ltd
See Also:
  • Field Summary

    Fields inherited from class java.util.EventObject

    source
  • Constructor Summary

    Constructors
    Constructor
    Description
    ComponentEvent(T source, boolean fromClient)
    Creates a new event using the given source and indicator whether the event originated from the client side or the server side.
    ComponentEvent(T source, boolean fromClient, UI ui)
    Creates a new event using the given source, indicator whether the event originated from the client side or the server side, and an explicit UI to associate with the event.
  • Method Summary

    Modifier and Type
    Method
    Description
     
    Gets the UI associated with this event.
    boolean
    Checks if this event originated from the client side.
    void
    Unregisters the event listener currently being invoked.

    Methods inherited from class java.util.EventObject

    toString

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • ComponentEvent

      public ComponentEvent(T source, boolean fromClient)
      Creates a new event using the given source and indicator whether the event originated from the client side or the server side.
      Parameters:
      source - the source component
      fromClient - true if the event originated from the client side, false otherwise
    • ComponentEvent

      public ComponentEvent(T source, boolean fromClient, UI ui)
      Creates a new event using the given source, indicator whether the event originated from the client side or the server side, and an explicit UI to associate with the event.

      Use this constructor when the source component may not be attached to a UI at the time the event is fired, but the UI is still known (for example, when the event is dispatched from code that has access to the current UI). The supplied UI is then returned by getUI() without relying on the source's attachment state.

      Parameters:
      source - the source component
      fromClient - true if the event originated from the client side, false otherwise
      ui - the UI associated with the event, or null if not available
  • Method Details

    • getSource

      public T getSource()
      Overrides:
      getSource in class EventObject
    • getUI

      public UI getUI()
      Gets the UI associated with this event.

      The UI is resolved in the following order:

      1. If a UI was explicitly provided at construction time, that instance is returned.
      2. If the source component is itself a UI, it is returned directly.
      3. Otherwise, the UI is obtained from getSource().getUI().get().

      In the common case the source is attached to a UI when the event fires, so this method is a convenient shorthand for getSource().getUI().get().

      If none of the above applies and the source component is not currently attached to a UI, this method throws an IllegalStateException. This can happen, for example, when an initial value is set on a field before it is added to the UI and a value-change listener is invoked. If your listener can run while the source is detached, use getSource().getUI() instead, which returns an Optional and lets you handle the detached case explicitly.

      Returns:
      the UI associated with this event, never null
      Throws:
      IllegalStateException - if the source component is not currently attached to a UI and no UI was provided at construction time
    • isFromClient

      public boolean isFromClient()
      Checks if this event originated from the client side.
      Returns:
      true if the event originated from the client side, false otherwise
    • unregisterListener

      public void unregisterListener()
      Unregisters the event listener currently being invoked.

      This method can only be called from within an event listener. Calling it will remove the current event listener so no further events are passed to it.