Wire Event Listeners

From Documentation
Revision as of 10:51, 7 February 2012 by Tomyeh (talk | contribs) (ZK 6)


Wire Event Listeners


Wire Event Listeners

To wire an event listener, you need to declare a method with @Listen annotation. The method should be public, with return type void, and has either no parameter or one parameter of the specific event type (corresponding to the event listened). The parameter of @Listen should be pairs of event name and selector, separated by semicolon.

For example,

@Listen("onClick = #btn0")
public void submit(MouseEvent event) {
	// called when onClick is received on the component of id btn0.
}
@Listen("onSelect = #listbox0")
public void select(SelectEvent event) {
	// called when onSelect is received on the component of id listbox0.
}

Event Listener Parameter

There are three ways to declare the method signature of the event listener:

  1. No parameter
  2. One parameter of the corresponding event type
  3. One parameter of a super class of the corresponding event type

For example,

@Listen("onChange = textbox#input0")
public void change() {
	// called when onChange is received on the textbox of id input0.
}
@Listen("onChange = textbox#input1")
public void change(InputEvent event) { 
	// called when onChange is received on the textbox of id input1.
}
@Listen("onChange = textbox#input2")
public void change(Event event) { 
	// called when onChange is received on the textbox of id input2.
}

Multiple Targets

If the selector matches multiple components, the event listener will be wired to all matched components. In such case, if you need to know which component receives the event, you can retrieve it from Event#getTarget().

For example,

@Listen("onClick = grid#myGrid > rows > row")
public void click(MouseEvent event) {
	// called when onClick is received on any Row directly under the Grid of id myGrid
}
@Listen("onClick = #btn0, #btn1, #btn2")
public void click(MouseEvent event) {
	// called when onClick is received on components of id #btn0, #btn1, or #btn2
}

Multiple Event Types

By separating multiple pairs of event names and selectors by semicolon, you can wire different types of event to a single method.

For example,

@Listen("onClick = button#submit; onOK = textbox#password")
public void submit(Event event) {
	// called when onClick is received on #submit, or onOK (Enter key pressed) is received on #password
}

Version History

Last Update : 2012/02/07


Version Date Content
6.0.0 February 2012 @Listen was introduced.



Last Update : 2012/02/07

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