Handling Events with Listeners

Let us put into practice what we learned of event handling in “Events and Listeners”. You can implement listener interfaces in a regular class, but it brings the problem with differentiating between different event sources. Using anonymous class for listeners is recommended in most cases.

Using Anonymous Classes

By far the easiest and the most common way to handle events in Java 6 and 7 is to use anonymous local classes. It encapsulates the handling of events to where the component is defined and does not require cumbering the managing class with interface implementations. The following example defines an anonymous class that inherits the Button.ClickListener interface.

Java

  1. // Have a component that fires click events
  2. final Button button = new Button("Click Me!");
  3. // Handle the events with an anonymous class
  4. button.addClickListener(new Button.ClickListener() {
  5. public void buttonClick(ClickEvent event) {
  6. button.setCaption("You made me click!");
  7. }
  8. });

See the on-line example.

Local objects referenced from within an anonymous class, such as the Button object in the above example, must be declared final.

Most components allow passing a listener to the constructor, thereby losing a line or two. However, notice that if accessing the component that is constructed from an anonymous class, you must use a reference that is declared before the constructor is executed, for example as a member variable in the outer class. If it is declared in the same expression where the constructor is called, it doesn’t yet exist. In such cases, you need to get a reference to the component from the event object.

Java

  1. final Button button = new Button("Click It!",
  2. new Button.ClickListener() {
  3. @Override
  4. public void buttonClick(ClickEvent event) {
  5. event.getButton().setCaption("Done!");
  6. }
  7. });

See the on-line example.

Handling Events in Java 8

Java 8 introduced lambda expressions, which offer a replacement for listeners. You can directly use lambda expressions in place of listeners that have only one method to implement.

For example, in the following, we use a lambda expression to handle button click events in the constructor:

Java

  1. layout.addComponent(new Button("Click Me!",
  2. event -> event.getButton().setCaption("You made click!")));

See the on-line example.

Java 8 is the future that is already here, and as Vaadin API uses event listeners extensively, using lambda expressions makes UI code much more readable.

Directing events to handler methods is easy with method references:

Java

  1. public class Java8Buttons extends CustomComponent {
  2. public Java8Buttons() {
  3. setCompositionRoot(new HorizontalLayout(
  4. new Button("OK", this::ok),
  5. new Button("Cancel", this::cancel)));
  6. }
  7. public void ok(ClickEvent event) {
  8. event.getButton().setCaption ("OK!");
  9. }
  10. public void cancel(ClickEvent event) {
  11. event.getButton().setCaption ("Not OK!");
  12. }
  13. }

See the on-line example.

Implementing a Listener in a Regular Class

The following example follows a typical pattern where you have a Button component and a listener that handles user interaction (clicks) communicated to the application as events. Here we define a class that listens to click events.

Java

  1. public class MyComposite extends CustomComponent
  2. implements Button.ClickListener {
  3. Button button; // Defined here for access
  4. public MyComposite() {
  5. Layout layout = new HorizontalLayout();
  6. // Just a single component in this composition
  7. button = new Button("Do not push this");
  8. button.addClickListener(this);
  9. layout.addComponent(button);
  10. setCompositionRoot(layout);
  11. }
  12. // The listener method implementation
  13. public void buttonClick(ClickEvent event) {
  14. button.setCaption("Do not push this again");
  15. }
  16. }

See the on-line example.

Differentiating Between Event Sources

If an application receives events of the same type from multiple sources, such as multiple buttons, it has to be able to distinguish between the sources. If using a regular class listener, distinguishing between the components can be done by comparing the source of the event with each of the components. The method for identifying the source depends on the event type.

Java

  1. public class TheButtons extends CustomComponent
  2. implements Button.ClickListener {
  3. Button onebutton;
  4. Button toobutton;
  5. public TheButtons() {
  6. onebutton = new Button("Button One", this);
  7. toobutton = new Button("A Button Too", this);
  8. // Put them in some layout
  9. Layout root = new HorizontalLayout();
  10. root.addComponent(onebutton);
  11. root.addComponent(toobutton);
  12. setCompositionRoot(root);
  13. }
  14. @Override
  15. public void buttonClick(ClickEvent event) {
  16. // Differentiate targets by event source
  17. if (event.getButton() == onebutton)
  18. onebutton.setCaption ("Pushed one");
  19. else if (event.getButton() == toobutton)
  20. toobutton.setCaption ("Pushed too");
  21. }
  22. }

See the on-line example.

Other techniques exist for separating between event sources, such as using object properties, names, or captions to separate between them. Using captions or any other visible text is generally discouraged, as it may create problems for internationalization. Using other symbolic strings can also be dangerous, because the syntax of such strings is checked only at runtime.