com.vaadin.ui.

Interface Component.Listener

  • All Superinterfaces:

    ConnectorEventListener, EventListener, Serializable, SerializableEventListener

    Enclosing interface:

    Component

    public static interface Component.Listener
    extends ConnectorEventListener

    Listener interface for receiving Component.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 Component.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, such as Button.ClickEvent.

     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:

    Component.addListener(Listener)

    • Method Summary

      All Methods
      Modifier and Type Method Description
      void componentEvent​(Component.Event event)

      Notifies the listener of a component event.

    • Method Detail

      • componentEvent

        void componentEvent​(Component.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.