ZK 弹出进度条

进度条应该是我们常用的功能,尤其是在大数据量处理,高强度计算的时候.下面就是一个小例子.
pg.zul页面代码
<?page title="Auto Generated index.zul"?>
<window id="tt1" title="Hello World!!" border="normal">
	<label value="点击,开始进度条的演示" />
	<button id="b1" label="弹出" onClick="pop()"></button>
	<zscript>
		public void pop() throws Exception{
			//弹出非模式进度条
			//win.setVisible(true);
			//弹出模式进度条
			win.doModal();
		}
	</zscript>
	<window id="win" visible="false" border="normal" title="进度条"
		width="300px" height="60px" mode="popup" apply="test.Test1">
		<vbox>
			<progressmeter id="info" value="0" width="290px" />
		</vbox>
	</window>
	<zscript><![CDATA[
		//必须要有,否则不能隐藏
		win.setVisible(false);
	]]></zscript>
</window>


后台JAVA关联代码
package test;

import org.zkoss.lang.Threads;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Desktop;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Progressmeter;

public class Test1 extends GenericForwardComposer {
	Progressmeter info;

	// 必须要有public构造方法
	public Test1() {
	}

	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		comp.setVariable(comp.getId() + "Ctrl", this, true);
		
		//启动进度条
		// enable server push
		desktop.enableServerPush(true);
		// invoke working thread and passing required component as parameter
		new Thread(new WorkingThread(info)).start();
	}

	public class WorkingThread implements Runnable {
		private final Desktop dt;
		private final Progressmeter pg;

		public WorkingThread(Progressmeter info) {
			dt = info.getDesktop();
			pg = info;
		}

		public void run() {
			try {
				for (int i = 0; i <= 100; i++) {
					// Update each 5 毫秒
					Threads.sleep(5);
					// get full control of desktop
					Executions.activate(dt);
					pg.setValue(i);
					Executions.deactivate(dt);
				}
				dt.enableServerPush(false);
			} catch (Exception ex) {
			}
		}
	}

	public Progressmeter getInfo() {
		return info;
	}

	public void setInfo(Progressmeter info) {
		this.info = info;
	}
}

你可能感兴趣的:(thread,UI,xml,ext,zk)