Event Listening

From Documentation

There are two ways to listen an event: an event handler and an event listener.

Listen by use of an Event Handler

An event handler is a method specified as an event attribute of a ZK page or as a member of a component class.

Declare an Event Handler in ZUML

An event handler can be declared in a ZUL page by specifying an event attribute[1]. For example,

<button label="hi" onClick='alert("Hello")'/>

where the content of the event handler is the code snippet in Java. The event handler will be interpreted at the run time (by use of BeanShell). If you prefer to use other language, you could specify the language name in front of it. For example, the following uses Groovy as the interpreter:

<button label="hi" onClick="groovy:alert('Hi, Groovy')"/>

Important Builtin Variables

  • self - the component receiving the event. In the previous example, it is the button itself.
  • event - the event being received. In the previous example, it is an instance of MouseEvent.

Notice that the event handler declared in this way is interpreted at the run time, so it inherits all advantages and disadvantage of interpreter-based execution.


  • It can be changed on the fly without recompiling and reloading the application.
  • Easy to maintain if the code snippet is small.


  • Slower to run.
  • Compilation error can not be known in advance.
  • Hard to maintain if mixing business logic with user interface.


  • It is generally suggested to use this approach for 1) prototyping, or 2) simple event handling.

Declare an Event Handler in Java

The other way to have an event listener is to declare it as a member of a component class. For example,

public class MyButton extends Button {
    public void onClick() {

If the event handler needs to handle the event, it can declare the event as the argument as follows.

public class MyButton extends Button {
    public void onClick(MouseEvent event) {
        Messagebox.show("Hello, "+event.getName());


  • It is suggested to use this approach for component development, since it is subtle for application developers to notice its existence. In additions, it requires to extend the component class.

  1. An event attribute is an attribute starting with on

Listen by Use of an Event Listener

Event Listener

An event listener is a class implementing EventListener. For example,

public class MyListener implements EventListener {
    public void onEvent(Event event) {

Then, you can register an event listener to the component that might receive the event by use of Component.addEventListener(String, EventListener). For example,

button.addEventListener("onClick", new MyListener());

This is a typical approach to handle events. However, it is a bit tedious to register event listeners one-by-one if there are a lot of listeners. Rather, it is suggested to use a composer as described in the following section.

Composer and Event Listener Autowiring

With ZK Developer's Reference/MVC, you generally don't need to register event listeners manually. Rather, they could be registered automatically by use of the auto-wiring feature of a composer. For example,

public class MyComposer extends GenericForwardComposer {
    public void onClick$hi() {
    public void onClick$bye() {
    public void onOK() {
        Messsagebox.show("OK pressed");

As shown above, the method for lsitening an event shall be named by starting with the event name, separating with $, and ending with the component's ID. The composer will search all matched methods and register the event listener automatically. Then, in the ZUL page, you can specify the apply attribute to associate the composer with a component.

<window apply="MyComposer">
    <button id="hi"/>
    <button id="bye"/>

If the listener needs to access the event, just declare it as the argument:

    public void onClick$hi(MouseEvent event) {
      Messsagebox.show("Hello, " + event.getName());

Though not limited, a composer is usually associated with an ID space (such as Window) to handle events and component within the give ID space. You could associate any component that properly represents a scope of your application to manage.

For more information please refer to the Wire Event Listeners section.

Deferrable Event Listeners

By default, events are sent to the server when it is fired at the client. However, many event listeners are just used to maintain the status at the server, rather than providing visual response to the user. In other words, the events for these listeners have no need to be sent immediately. Rather, they shall be sent at once to minimize the traffic between the client and the server, and then to improve the server's performance. For the sake of the description convenience, we call them the deferrable event listeners.

To make an event listener deferrable, you have to implement the org.zkoss.zk.ui.event.Deferrable interface (with EventListener) and return true for the isDeferrable method as follows.

 public class DeferrableListener implements EventListener, Deferrable {
     private boolean _modified;
     public void onEvent(Event event) {
         _modified = true;
     public boolean isDeferrable() {
         return true;

When an event is fired at the client (e.g., the user selects a list item), ZK won't send the event if no event listener is registered for it or only deferrable listeners are registered. instead, the event is queued at the client.

On the hand, if at least one non-deferrable listener is registered, the event are sent immediately with all queued events to the server at once. No event is lost and the arriving order is preserved.

Tip: Use the deferrable listeners for maintaining the server status, while the non-deferrable listeners for providing the visual responses for the user.

Precedence of Listeners

The precedence of listeners highest to lowest is as follows.

Abort the Invocation Sequence

You could abort the calling sequence by calling Event.stopPropagation(). Once one of the event listeners invokes this method, all following event handlers and listeners are ignored.

Version History

Version Date Content
5.0.6 November 2010 SerializableEventListener was introduced to simply the instantiation of a serializable anonymous class

Last Update : 2010/12/02

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.