# Wiring event listeners from a template

s3lvatico
22 2

Greetings, everyone.

I have a Listbox inside a Window controlled by a SelectorComposer. The Listbox is populated by a model coming from the composer, and in the last column I placed a Button. (in this way, each row in the listbox has a button with a unique id).

<window id="wndMain" apply="com.example.MainComposer">
...
<listbox model="${wndMain$composer.leModel}" id="lbx">
...
<template name="model">
<listitem>
<listcell label="${each.code}" /> ... <listcell> <button id="${each.code}" label="Make Active" class="selectionButton"  />
</listcell>
</listitem>
</template>
</listbox>
</window>


Now, I'd like to wire the onClick event of each button with a single event listener defined inside the composer.

I tried the following approach inside the composer:

@Listen("onClick = button.selectionButton")
public void selectStuff(MouseEvent evt) {
Component target = evt.getTarget();
String clickedRowCode = target.getId();
System.out.println("you clicked on: " + clickedRowCode);
}


but at runtime there is no interaction between the components and the composer.

I imagine that the cause of this is that the actual wiring happens during the invocation of super.doAfterCompose(...) and there is no wiring done when the listbox rows are dynamically created.

So I'd like to do the other way around, that is referencing the event handler of the composer directly from the ZUL file.

What is the correct syntax to use in this case?

<button id="${each.code}" label="Make Active" onClick="????" />  delete retag edit ## 1 Answer Sort by » oldest newest most voted answered 2013-10-14 02:28:04 +0800 dennis 3669 1 6 http://www.javaworld.com.... the wiring in doing when afterCompose and at that time, there is no listitem/button yet. I suggest you to forward the onClick event of button (in each listitem) to the listbox like <button id="${each.code}" label="Make Active" class="selectionButton" onClick='lbx.onSomeClick'  />


And in composer

@Listen("onSomeClick = #lbx")
public void selectStuff(ForawrdEvent evt) {}

[hide preview]