Interface Listener

All Superinterfaces:
com.vaadin.flow.component.ComponentEventListener<Event>, EventListener, Serializable

public interface Listener extends com.vaadin.flow.component.ComponentEventListener<Event>
Legacy version of Listener interface that resembles Vaadin 7/8's Component.Listener API as closely as possible in order to facilitate migration to newer versions of Vaadin. Listener interface for receiving Events.

Listener interfaces are the basis of all user interaction handling in Vaadin. You have or create a listener object that receives the events. All event types have their corresponding listener types; they are not, however, required to inherit the Listener interface, and they rarely do so.

This generic listener interface is useful typically when you wish to handle events from different component types in a single listener method (componentEvent(). If you handle component events in an anonymous listener class, you normally use the component specific listener class.

 class Listening extends CustomComponent implements Listener {
     Button ok; // Stored for determining the source of an event

     Label status; // For displaying info about the event

     public Listening() {
         VerticalLayout layout = new VerticalLayout();

         // Some miscellaneous component
         TextField name = new TextField("Say it all here");
         name.addListener(this);
         layout.addComponent(name);

         // Handle button clicks as generic events instead
         // of Button.ClickEvent events
         ok = new Button("OK");
         ok.addListener(this);
         layout.addComponent(ok);

         // For displaying information about an event
         status = new Label("");
         layout.addComponent(status);

         setCompositionRoot(layout);
     }

     public void componentEvent(Event event) {
         // Act according to the source of the event
         if (event.getSource() == ok
                 && event.getClass() == Button.ClickEvent.class)
             getWindow().showNotification("Click!");

         // Display source component and event class names
         status.setValue(
                 "Event from " + event.getSource().getClass().getName() + ": "
                         + event.getClass().getName());
     }
 }

 Listening listening = new Listening();
 layout.addComponent(listening);
 
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Notifies the listener of a component event.
    default void
     
  • Method Details

    • componentEvent

      void componentEvent(Event event)
      Notifies the listener of a component event.

      As the event can typically come from one of many source components, you may need to differentiate between the event source by component reference, class, etc.

       public void componentEvent(Event event) {
           // Act according to the source of the event
           if (event.getSource() == ok
                   && event.getClass() == Button.ClickEvent.class)
               getWindow().showNotification("Click!");
      
           // Display source component and event class names
           status.setValue("Event from " + event.getSource().getClass().getName()
                   + ": " + event.getClass().getName());
       }
       
      Parameters:
      event - the event that has occurred.
    • onComponentEvent

      default void onComponentEvent(Event event)
      Specified by:
      onComponentEvent in interface com.vaadin.flow.component.ComponentEventListener<Event>