You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

architecture-events.asciidoc 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. ---
  2. title: Events and Listeners
  3. order: 4
  4. layout: page
  5. ---
  6. [[architecture.events]]
  7. = Events and Listeners
  8. Vaadin Framework offers an event-driven programming model for handling user interaction.
  9. When a user does something in the user interface, such as clicks a button or
  10. selects an item, the application needs to know about it. Many Java-based user
  11. interface frameworks follow the __Event-Listener pattern__ (also known as the
  12. Observer design pattern) to communicate user input to the application logic. So
  13. does Vaadin Framework. The design pattern involves two kinds of elements: an object that
  14. generates ("fires" or "emits") events and a number of listeners that listen for
  15. the events. When such an event occurs, the object sends a notification about it
  16. to all the listeners. In a typical case, there is only one listener.
  17. Events can serve many kinds of purposes. In Vaadin Framework, the usual purpose of events
  18. is handling user interaction in a user interface. Session management can require
  19. special events, such as time-out, in which case the event would actually be the
  20. lack of user interaction. Time-out is a special case of timed or scheduled
  21. events, where an event occurs at a specific date and time or when a set time has
  22. passed.
  23. To receive events of a particular type, an application must register a listener
  24. object with the event source. The listeners are registered in the components
  25. with an [methodname]#add*Listener()# method (with a method name specific to the
  26. listener).
  27. Most components that have related events define their own event class and the
  28. corresponding listener class. For example, the [classname]#Button# has
  29. [classname]#Button.ClickEvent# events, which can be listened to through the
  30. [classname]#Button.ClickListener# functional interface.
  31. In the following, we assign a button click listener using a lambda expression.
  32. [source, java]
  33. ----
  34. final Button button = new Button("Push it!");
  35. button.addClickListener(event ->
  36. button.setCaption("You pushed it!"));
  37. ----
  38. <<figure.eventlistenerdiagram>> illustrates the case where an
  39. application-specific class inherits the [classname]#Button.ClickListener#
  40. interface to be able to listen for button click events. The application must
  41. instantiate the listener class and register it with
  42. [methodname]#addClickListener()#. When an event occurs, an event object is instantiated, in this case a
  43. [classname]#Button.ClickEvent#. The event object knows the related UI component,
  44. in this case the [classname]#Button#.
  45. [[figure.eventlistenerdiagram]]
  46. .Class Diagram of a Button Click Listener
  47. image::img/events-classdiagram-hi.png[width=80%, scaledwidth=100%]
  48. <<dummy/../../../framework/application/application-events#application.events,"Handling Events with Listeners">> goes into details of handling events in practice.