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 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. ---
  2. title: Events and Listeners
  3. order: 4
  4. layout: page
  5. ---
  6. [[architecture.events]]
  7. = Events and Listeners
  8. Vaadin 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. 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, 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# interface.
  31. In the following, we handle button clicks with a listener implemented as an
  32. anonymous class:
  33. [source, java]
  34. ----
  35. final Button button = new Button("Push it!");
  36. button.addClickListener(new Button.ClickListener() {
  37. public void buttonClick(ClickEvent event) {
  38. button.setCaption("You pushed it!");
  39. }
  40. });
  41. ----
  42. <<figure.eventlistenerdiagram>> illustrates the case where an
  43. application-specific class inherits the [classname]#Button.ClickListener#
  44. interface to be able to listen for button click events. The application must
  45. instantiate the listener class and register it with
  46. [methodname]#addClickListener()#. It can be an anonymous class, such as the one
  47. above. When an event occurs, an event object is instantiated, in this case a
  48. [classname]#Button.ClickEvent#. The event object knows the related UI component,
  49. in this case the [classname]#Button#.
  50. [[figure.eventlistenerdiagram]]
  51. .Class Diagram of a Button Click Listener
  52. image::img/events-classdiagram-hi.png[width=50%, scaledwidth=75%]
  53. In Java 8, you can implement such functional interfaces with a lambda expression:
  54. [source, java]
  55. ----
  56. Button button = new Button("Push it!");
  57. button.addClickListener(event ->
  58. button.setCaption("You pushed it!"));
  59. ----
  60. In the ancient times of C programming, __callback functions__ filled largely the
  61. same need as listeners do now. In object-oriented languages, we usually only
  62. have classes and methods, not functions, so the application has to give a class
  63. interface instead of a callback function pointer to the framework.
  64. <<dummy/../../../framework/application/application-events#application.events,"Handling Events with Listeners">> goes into details of handling events in practice.