event handler"

From Documentation
m (Created page with '{{ZKDevelopersGuidePageHeader}} You can write event handler code in <tt>zscript</tt> or <tt>forward</tt> to event handler in java. If the code is really simple, you can write i…')
 
m (correct highlight (via JWB))
 
Line 1: Line 1:
 
{{ZKDevelopersGuidePageHeader}}
 
{{ZKDevelopersGuidePageHeader}}
  
You can write event handler code in <tt>zscript</tt> or <tt>forward</tt> to event handler in java.
+
You can write event handler code in <code>zscript</code> or <code>forward</code> to event handler in java.
  
If the code is really simple, you can write it in <tt>zscript</tt> following event's name. Like following example:
+
If the code is really simple, you can write it in <code>zscript</code> following event's name. Like following example:
 
<source lang="xml" >
 
<source lang="xml" >
 
<window>
 
<window>
Line 10: Line 10:
 
</source>
 
</source>
  
If the code is more than one line, for readability, you can use <tt>attribute</tt>.
+
If the code is more than one line, for readability, you can use <code>attribute</code>.
 
<source lang="xml" >
 
<source lang="xml" >
 
<window>
 
<window>
Line 21: Line 21:
 
</source>
 
</source>
  
Or you can deliberately define a method in <tt>zscript</tt>, and call it from event's following <tt>zscript</tt>.
+
Or you can deliberately define a method in <code>zscript</code>, and call it from event's following <code>zscript</code>.
 
<source lang="xml" >
 
<source lang="xml" >
 
<window>
 
<window>
Line 34: Line 34:
 
</source>
 
</source>
  
To handle events in java, you define static java method, or use zk attributes: <tt>use</tt> or <tt>apply</tt>.
+
To handle events in java, you define static java method, or use zk attributes: <code>use</code> or <code>apply</code>.
  
 
The following example shows how to call static method in java,  
 
The following example shows how to call static method in java,  
Line 59: Line 59:
 
</source>
 
</source>
  
The following example uses zk attribute: <tt>use</tt> ,
+
The following example uses zk attribute: <code>use</code> ,
 
<source lang="xml" >
 
<source lang="xml" >
 
<window id="win_1" use="MyWindow">
 
<window id="win_1" use="MyWindow">
Line 81: Line 81:
 
</source>
 
</source>
  
The following example uses zk attribute: <tt>apply</tt>. Note that name of event handling method must be <tt>onXXX</tt>.
+
The following example uses zk attribute: <code>apply</code>. Note that name of event handling method must be <code>onXXX</code>.
  
 
<source lang="xml" >
 
<source lang="xml" >

Latest revision as of 10:41, 19 January 2022

Stop.png This documentation is for an older version of ZK. For the latest one, please click here.


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();
		}	
	}
}



Last Update : 2022/01/19

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