Using Events with Components

Your component class can provide an event-handling API that uses the event bus provided by the Component base class.

The event bus supports:

  • Event classes that extend ComponentEvent, and

  • Listeners of the type ComponentEventListener<EventType>.

Defining an Event

To use the event bus, your event should extend ComponentEvent. The base type is parameterized with the type of the component firing the event. This means that the getSource() method automatically returns the correct component type.

The second constructor parameter determines whether the event is triggered by a DOM event in the browser or through the component’s server-side API.

Example: Creating an event by extending ComponentEvent.

Java

  1. public class ChangeEvent
  2. extends ComponentEvent<TextField> {
  3. public ChangeEvent(TextField source,
  4. boolean fromClient) {
  5. super(source, fromClient);
  6. }
  7. }

Defining an Event Listener

Event listeners are of the generic ComponentEventListener<EventType> type, so it is not necessary to create a separate interface for your event type.

In addition, the method for adding a listener returns a handle that can be used to remove the listener, so it is unnecessary to implement a separate method to remove an event listener.

Example: Using the addChangeListener method to add an event listener.

Java

  1. @Tag("input")
  2. public class TextField extends Component {
  3. public Registration addChangeListener(
  4. ComponentEventListener<ChangeEvent> listener) {
  5. return addListener(ChangeEvent.class, listener);
  6. }
  7. // Other component methods omitted
  8. }

Example: Adding and removing event listeners.

Java

  1. TextField textField = new TextField();
  2. Registration registration = textField
  3. .addChangeListener(e ->
  4. System.out.println("Event fired"));
  5. // In some other part of the code
  6. registration.remove();

Firing Events from the Server

You can fire an event on the server by creating the event instance and passing it to the fireEvent method. Use false as the second constructor parameter to indicate that the event does not come from the client.

Example: Using the fireEvent method set to false to fire an event from the server.

Java

  1. @Tag("input")
  2. public class TextField extends Component {
  3. public void setValue(String value) {
  4. getElement().setAttribute("value", value);
  5. fireEvent(new ChangeEvent(this, false));
  6. }
  7. // Other component methods omitted
  8. }

Firing Events From the Client

You can connect a component event to a DOM event that is fired by the element in the browser.

To do this, use the @DomEvent annotation in your event class to specify the name of the DOM event to listen to. Vaadin Flow automatically adds a DOM event listener to the element when a component event listener is present.

Example: Using the @DomEvent annotation to connect TextField component to a DOM event.

Java

  1. @DomEvent("change")
  2. public class ChangeEvent
  3. extends ComponentEvent<TextField> {
  4. public ChangeEvent(TextField source,
  5. boolean fromClient) {
  6. super(source, fromClient);
  7. }
  8. }

Adding Event Data

An event can include additional information, for example the mouse button used for a click event.

The @DomEvent annotation supports additional constructor parameters. You can use the @EventData annotation to define which data to send from the browser.

Example: Using the @EventData annotation to define additional click-event data.

Java

  1. @DomEvent("click")
  2. public class ClickEvent
  3. extends ComponentEvent<NativeButton> {
  4. private final int button;
  5. public ClickEvent(NativeButton source,
  6. boolean fromClient,
  7. @EventData("event.button") int button) {
  8. super(source, fromClient);
  9. this.button = button;
  10. }
  11. public int getButton() {
  12. return button;
  13. }
  14. }
  • The @EventData definition runs as JavaScript in the browser.

  • The DOM event is available as event and the element to which the listener was added is available as element.

  • See DomListenerRegistration.addEventData in the Javadoc for more about how event data is collected and sent to the server.

Tip
See Event in the MDN web docs for an overview of standard DOM events and properties.

Filtering Events

Instead of sending all DOM events to the server, you can filter events by defining a filter in the @DomEvent annotation. The filter is typically based on things related to the event.

Example: Defining a filter in the @DomEvent annotation.

Java

  1. @DomEvent(value = "keypress",
  2. filter = "event.key == 'Enter'")
  3. public class EnterPressEvent
  4. extends ComponentEvent<TextField> {
  5. public EnterPressEvent(TextField source,
  6. boolean fromClient) {
  7. super(source, fromClient);
  8. }
  9. }
  • The filter definition runs as JavaScript in the browser.

  • The DOM event is available as event and the element to which the listener was added is available as element.

  • See DomListenerRegistration.setFilter in the Javadoc for more about how the filter is used.

Limiting Event Frequency

Certain kinds of events are fired very frequently when the user interacts with the component. For example, text input events fired while the user types.

You can configure the rate at which events are sent to the server by defining different debounce settings in the @DomEvent annotation. Debouncing always requires a timeout (in milliseconds) and a burst phase, which determines when events are sent to the server. There are three burst phase options:

  • LEADING phase: An event is sent at the beginning of a burst, but subsequent events are only sent after one timeout period has passed, without any new events. This is useful for things like button clicks to prevent accidental double submissions.

  • INTERMEDIATE phase: Periodical events are sent while a burst is ongoing. Subsequent events are delayed until one timeout period since the last event has passed. This is useful for things like text input, if you want to react continuously while the user types.

  • TRAILING phase: This phase is triggered at the end of a burst after the timeout period has passed without any further events. This is useful for things like text input if you want to react only when the user stops typing.

Example: Configuring an input event to be sent to the server half a second after the user’s last input.

Java

  1. @DomEvent(value = "input",
  2. debounce = @DebounceSettings(
  3. timeout = 250,
  4. phases = DebouncePhase.TRAILING))
  5. public class InputEvent
  6. extends ComponentEvent<TextField> {
  7. private String value;
  8. public InputEvent(TextField source,
  9. boolean fromClient,
  10. @EventData("element.value") String value) {
  11. super(source, fromClient);
  12. this.value = value;
  13. }
  14. public String getValue() {
  15. return value;
  16. }
  17. }

You can configure active events for several phases at the same time.

Example: Configuring an event for both the LEADING phase (immediately when a burst starts) and the INTERMEDIATE phase (while the burst is ongoing).

Java

  1. @DomEvent(value = "input",
  2. debounce = @DebounceSettings(
  3. timeout = 500,
  4. phases = {DebouncePhase.LEADING,
  5. DebouncePhase.INTERMEDIATE }))
  6. public class ContinuousInputEvent
  7. extends ComponentEvent<TextField> {
  8. private String value;
  9. public ContinuousInputEvent(TextField source,
  10. boolean fromClient,
  11. @EventData("element.value") String value) {
  12. super(source, fromClient);
  13. this.value = value;
  14. }
  15. public String getValue() {
  16. return value;
  17. }
  18. }
Note
If you configure a filter and a debounce rate, only events that pass the filter are considered when determining whether a burst has ended.