From Documentation

Jump to: navigation, search



You can write event handler code in zscript or forward to event handler in java.

If the code is really simple, you can write it in zscript following event's name. Like following example:

<window>
	<button onClick='alert("here is a zcript")'/>
</window>

If the code is more than one line, for readability, you can use attribute.

<window>
	<button>
		<attribute name="onClick">
			alert("here is a zscript");
		</attribute>
	</button>
</window>

Or you can deliberately define a method in zscript, and call it from event's following zscript.

<window>
	<zscript><![CDATA[
		public void showAlert(){
			alert("here is a zcript too");
		}
	]]>
	</zscript>	
	<button onClick="showAlert()"/>
</window>

To handle events in java, you define static java method, or use zk attributes: use or apply.

The following example shows how to call static method in java,

<window>
	<button onClick="MyManager.showAlert()"/>
</window>

And MyManager.java

import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class MyManager {
	public static void showAlert(){
		try {
			Messagebox.show("handle event in java");
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
}

The following example uses zk attribute: use ,

<window id="win_1" use="MyWindow">
	<button onClick="win_1.showAlert()"/>
</window>
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class MyWindow extends Window {
	public void showAlert(){
		try {
			Messagebox.show("handle event in java");
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
}

The following example uses zk attribute: apply. Note that name of event handling method must be onXXX.

<window apply="MyComposer">
	<button forward="onShowAlert()"/>
</window>
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.GenericComposer;
import org.zkoss.zul.Messagebox;

public class MyComposer extends GenericComposer {
	public void onShowAlert(Event evt) {
		try {
			Messagebox.show("handle event in java");
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
}




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