0

Java method access from zul file

asked 2011-07-11 14:05:00 +0800

airton gravatar image airton
78 1

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.

delete flag offensive retag edit

8 Replies

Sort by ยป oldest newest

answered 2011-07-11 14:37:41 +0800

caclark gravatar image caclark
1753 2 5
http://clarktrips.intltwi...

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.

link publish delete flag offensive edit

answered 2011-07-12 09:33:51 +0800

airton gravatar image airton
78 1

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.

link publish delete flag offensive edit

answered 2011-07-12 10:15:03 +0800

caclark gravatar image caclark
1753 2 5
http://clarktrips.intltwi...

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
	}
	
}

link publish delete flag offensive edit

answered 2011-07-12 11:16:17 +0800

twiegand gravatar image twiegand
1807 3

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

link publish delete flag offensive edit

answered 2011-07-12 12:35:39 +0800

airton gravatar image airton
78 1

Cary,

I've got: org.zkoss.zk.ui.UiException: Not an event name: myMethod


Todd,

On Java class: event cannot be resolved


How to solve them?

Thanks.

link publish delete flag offensive edit

answered 2011-07-12 13:14:16 +0800

twiegand gravatar image twiegand
1807 3

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

link publish delete flag offensive edit

answered 2011-07-12 14:47:59 +0800

airton gravatar image airton
78 1

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.

link publish delete flag offensive edit

answered 2011-07-12 16:05:24 +0800

caclark gravatar image caclark
1753 2 5
http://clarktrips.intltwi...

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)...

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2011-07-11 14:05:00 +0800

Seen: 446 times

Last updated: Jul 12 '11

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More