-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hello.
How to achieve something like that?
.zul file:
<window id="win" apply="MyJavaClass" > <textbox id="tb1" onChange='myMethod(self)'/> <textbox id="tb2" onChange='myMethod(self)'/> <textbox id="tb3" onChange='myMethod(self)'/> </window>
.java file:
public class MyJavaClass() extends GenericForwardComposer{ public void myMethod(Textbox tb){ String valueOfTextBox_n = tb.getValue(); } }
Thanks a lot.
try something like this:
zul file:
<window id="win" apply="MyJavaClass" > <textbox id="tb1" /> <textbox id="tb2" /> <textbox id="tb3" /> </window>
java file:
public class MyJavaClass() extends GenericForwardComposer{ public void onChange$tb1(ForwardEvent event){ String valueOfTextBox_1 = event.getReference().getValue(); } public void onChange$tb2(ForwardEvent event){ String valueOfTextBox_2 = event.getReference().getValue(); } public void onChange$tb3(ForwardEvent event){ String valueOfTextBox_3 = event.getReference().getValue(); } }
You might have to mess around with the ForwardEvent methods and cast them to the correct class first, buy you can get the general idea from my example.
I have 70 components on my zul file.
So I was trying to create a more generic method; I wouldn't to create one method for each component.
I tried a class that implements Composer but it didn't work if it extends GenericForwardComposer too, like so:
public class MyJavaClass() extends GenericForwardComposer implements Composer {
Implementing Composer could be my solution:
public class MyClass implements Composer { @Override public void doAfterCompose(final Component target) { if (target instanceof Button){ target.addEventListener("onClick", new EventListener() { public void onEvent(Event event) { System.out.println("clicked button: " + target.getId()); } }); } if (target instanceof Textbox){ target.addEventListener("onChange", new EventListener() { public void onEvent(Event event) { ... String theValue = ((Textbox)target).getValue(); ... } }); } } }
but I have data on my first class (that extends GenericForwardComposer) that I need on my second class (that implements Composer). I tried to put these data on desktop (desktop.setAttribute...) to retrieve them on second class but there I couldn't do that... desktop and session is not available on a class that implements Composer...
Thanks for your help.
in that case, you can check out the forward attribute:
<window id="win" apply="MyJavaClass" > <textbox id="tb1" forward="onChange=win.myMethod(event)" /> <textbox id="tb2" forward="onChange=win.myMethod(event)" /> <textbox id="tb3" forward="onChange=win.myMethod(event)" /> </window>
this forwards the onChange event of each textbox to your window's myMethod(ForwardEvent event) method that should look like this:
public class MyJavaClass() extends GenericForwardComposer{ public void myMethod(ForwardEvent event){ Textbox tb = (Textbox)event.getReference() ; // do what you need with the textbox } }
Here is another approach using forwarded events:
public class MyJavaClass extends GenericForwardComposer { public void onChangeTextbox(){ Textbox tb = (Textbox)event.getOrigin().getTarget(); System.out.println("Textbox " + tb.getId() + " has a value of: " + tb.getValue()); } }
<window id="win" apply="MyJavaClass" > <textbox id="tb1" forward="onChange=onChangeTextbox()" /> <textbox id="tb2" forward="onChange=onChangeTextbox()" /> <textbox id="tb3" forward="onChange=onChangeTextbox()" /> </window>
Regards,
Todd
airton,
If you put the following code in your environment, does it work?
<zk> <zscript> import org.zkoss.zk.ui.util.GenericForwardComposer; public class MyJavaClass extends GenericForwardComposer { public void onChangeTextbox(){ Textbox tb = (Textbox)event.getOrigin().getTarget(); System.out.println("Textbox " + tb.getId() + " has a value of: " + tb.getValue()); } } </zscript> <window id="win" apply="MyJavaClass" > <textbox id="tb1" forward="onChange=onChangeTextbox()" /> <textbox id="tb2" forward="onChange=onChangeTextbox()" /> <textbox id="tb3" forward="onChange=onChangeTextbox()" /> </window> </zk>
Also, which version of ZK are you running?
Todd
Hi, Todd.
Yes, it works on script. But if I use it on a Java class, I have to rename the method to "public void onChangeTextbox(ForwardEvent event)".
I've done that and it's ok.
The other problem I've found: event cannot be resolved
The method name must initiate by "on"... so, changing the method name suggested by Cary from myMethod to onMyMethod, for example, it works! Important: observe camel case or it won't works (onMyMethod).
Thank you all.
ah, yes, I forgot the naming convention...that's the correct fix.
Todd's didn't work because "event" wasn't defined in onChangeTextbox()...it needed to be onChangeTextbox(Event event)...
Asked: 2011-07-11 14:05:00 +0800
Seen: 446 times
Last updated: Jul 12 '11