New ZK Demo Introduction

From Documentation
DocumentationSmall Talks2008JanuaryNew ZK Demo Introduction
New ZK Demo Introduction

Ryan Wu, Engineer, Potix Corporation
Jan 20, 2008
Since ZK 3.5.2



This article is created for those who are interested in the architecture of the latest ZK Demo.

The ZK Demo is one of the most important parts of ZK's website. In this article, I will introduce how the new demo was created and what type of ZK features was used.
You are recommended to download the source and read it with this article.

The UI Part


First, We create a borderlayout to construct the whole main page. It contains three parts.

  • North: The category selection bar which is made by a macro components named category and a customized component, categorybar.
  • West: The case list which we use listmodel to generate and replace the data.
  • Center: The case content. If a demo case is selected in the west list, this part will show the demo case's content and it's sourced by including the corresponding zul file. Each of them shows data in a Tabbox.
<borderlayout id="main" apply="org.zkoss.zkdemo.userguide.MainLayoutComposer">
  <north border="none" size="100px" sclass="demo-header" collapsible="true">
  <west title="ZK ${desktop.webApp.version} Live Demo" size="250px" flex="true" splittable="true"
    minsize="210" maxsize="500" collapsible="true">
  <center autoscroll="true" flex="true">
  • Namespace
    • BorderLayout : main

You might be confused why the borderlayout(main) could have some special functions, for example

  1. <category forEach="${main.categories}" />
  2. <listbox model="${main.selectedModel}" ></listbox>

Let's take a look at the composer[1] of borderlayout, MainLayoutComposer. We used the FusionInvoker[2] and bind the composer object to original borderlayout, main.

public class MainLayoutComposer extends GenericForwardComposer implements MainLayoutAPI, ComposerExt {
  public void doBeforeComposeChildren(Component comp) throws Exception {
    Object obj = FusionInvoker.newInstance(new Object[] { comp, this });
    comp.setVariable("main", obj, true);
    main = (Borderlayout) comp;

The Category Bar

Zkdemo category.PNG This part contains two elements, category and category bar.


This is a customized component, which extend the Div component. We created it and used forEach to load the categories. When the screen size is less than the width of the categories, users can use the scroll button and scroll to the hidden categories.

The client side function is written in the index.zul with a JavaScript Class zkCategoryBar.

zkCategoryBar = {
  init: function (cmp) { ... },
  onSize: function (cmp) { ... },
  _forceStyle: function (cmp, value) { ... },
  _isLegalType: function (n) { ... },
  _checkScrolling: function (cmp, body) { ... },
  _fixButton : function(cmp) { ... },
  onScrollTo: function (cmp) { ... },
  onClickArrow: function (evt) { ... },
  stop: function () { .. },
  scroll: function(cmp, move, btn, isRight, stopPropagate) { ... },
  goscroll: function(body, isRight, step) { ... }

The categorybar's code is in /userguide/macros/categorybar.dsp

<%@ taglib uri="" prefix="c" %>
<%@ taglib uri="" prefix="z" %>
<c:set var="self" value="${requestScope.arg.self}"/>
<c:set var="uuid" value="${self.uuid}"/>
<div id="${uuid}" z.type="CategoryBar" ${self.outerAttrs}${self.innerAttrs}>
<div id="${uuid}!right"></div>
<div id="${uuid}!left"></div>
  <div id="${uuid}!body" class="${self.zclass}-body">
  <div id="${uuid}!cave">
    <c:forEach var="child" items="${self.children}">${z:redraw(child, null)}</c:forEach>
    <div class="z-clear"></div>

For more information or guide about creating customized components in ZK, please refer to ZK Component Development Essentials.
We first define the categorybar, and use it in the index.zul.

<?component name="categorybar" extends="div" moldURI="/userguide/macros/categorybar.dsp"?>
<categorybar zclass="demo-categorybar" id="header">
  <category forEach="${main.categories}" id="${}" src="${each.icon}" label="${each.label}"/>

The main.categories reflect to the composer's getCategories(). It will load the categories form

public Category[] getCategories() {
  return (Category[]) getCategoryMap().values().toArray(new Category[] {});
private Map getCategoryMap() {
  return DemoWebAppInit.getCateMap();

The DemoWebAppInit is the java class use to load the property file -

public class DemoWebAppInit implements WebAppInit {
  private static final Log log = Log.lookup(DemoWebAppInit.class);
  final static String PATH = "/userguide/";
  final static String CONFIG = "";
  final static String CATEGORY_TYPE = "CATEGORY";
  final static String LINK_TYPE = "LINK";
  private static Map _cateMap = new LinkedHashMap () { ... };
  public void init(WebApp wapp) throws Exception { ... }
  static Map getCateMap() { ... }
  private void loadProperites(ServletContext context) { ... }


This is an macro components, which will show as an icon. (The data of categories was loaded from DemoWebAppInit.)

<div id="${}" style="" sclass="demo-category pointer" forward="main.onCategorySelect"
  action="onmouseover:zk.addClass(this, 'demo-over'); onmouseout:zk.rmClass(this, 'demo-over'); onclick:if(zkau.insamepos(event)) onSelect(this)">
  <image sclass="demo-category-img" tooltiptext="${arg.label}" src="${arg.src}" action="onmousedown:return false;"/>
  <separator height="1px"/>
  <label sclass="demo-category-text" value="${arg.label}"/>

It will forward[3] the select event to composer's onCategorySelect().
We create a Category class to save data.

public class Category {
  private String _id;
  private String _icon;
  private String _label;
  private String _href;
  private List _items;
  public Category(String id, String icon, String label, String href) {...}
  public void addItem(DemoItem item) {...}
  public String getHref() {...}
  public List getItems() {...}
  public String getId() {...}
  public String getIcon() {...}
  public String getLabel() {...}

The Demo Case List

Zkdemo caselist.PNG There's a toolbar with search function and a listbox with case list in this part.

  <label value="Search:"/><textbox id="searchBox" ctrlKeys="#down#up" focus="true" sclass="demo-search-inp"/>
  <listbox id="itemList" oddRowSclass="non-odd" sclass="demo-items"
   itemRenderer="${main.itemRenderer}" model="${main.selectedModel}" fixedLayout="true" vflex="true">   
  • Namespace
    • Textbox : searchbox
    • Listbox : itemList

The main.itemRenderer and main.selectedModel reflect to the composer's getItemRenderer() and getSelectedModel().

public ListModel getSelectedModel() {
  Category cate = _selected == null ? getCategories()[0] :
  return new ListModelList(cate.getItems());
public ListitemRenderer getItemRenderer() {
  return _defRend;

The Case Content

For each case there's a window which applies composer, org.zkoss.zkdemo.userguide.DemoWindowComposer. ZKdemo case.png

<window id="demo" apply="org.zkoss.zkdemo.userguide.DemoWindowComposer">
  <tabbox width="100%" tabscroll="false">
      <tab id="demoView" label="Demo"/>
      <tab id="srcView" label="View Source"/>
        <window id="view">
            <textbox id="codeView" class="code" rows="20" width="95%">
      <attribute name="value"><![CDATA[
            <!-- THE DEMO ZUL CODE -->
          <toolbar mold="panel">
            <button id="tryBtn" label="Try me!"/>
            <button id="reloadBtn" label="Reload" height="18px"/>

The composer used to handle the "reload" and "Try me" button, and create a div included the font/theme change button.

public class DemoWindowComposer extends GenericForwardComposer {
	Window view;
	Tab demoView;
	Textbox codeView;
	Button reloadBtn;
	Button tryBtn;
	public void doAfterCompose(Component comp) throws Exception {
		final Div inc = new Div();
		Executions.createComponents("/userguide/bar.zul", inc, null);
		if (Library.getProperty("org.zkoss.zkdemo.theme.silvergray") != null) {
		comp.insertBefore(inc, comp.getFirstChild());
		if (view != null) execute();
	public void execute() { ... }
	public void onClick$reloadBtn(Event event) { ... }
	public void onClick$tryBtn(Event event) { ... }

The Composer Part

The composer of borderlayout is MainLayoutComposer. It extends the GenericForwardComposer[4].

The "$" will auto forward the onBookmarkChange event to borderlayout, main , the onSelect event to the itemList, the onCtrlKey event and onChanging event to searchBox.

public class MainLayoutComposer extends GenericForwardComposer implements MainLayoutAPI, ComposerExt {
        /** Event  */

	/* Init function of main */
	public void onMainCreate(Event event) {...}
	/*Category select*/
	public void onCategorySelect(ForwardEvent event) {...}
        /* Bookmark change of main */
	public void onBookmarkChange$main(BookmarkEvent event) {...}
        /* Select itemList */
	public void onSelect$itemList(SelectEvent event) {...}
        /* Catch arrow key "Up" and "Down" from users */
	public void onCtrlKey$searchBox(KeyEvent event) {...}
        /* Search demo during input */
	public void onChanging$searchBox(InputEvent event) {...}

        /** UI Control */

	public Category[] getCategories() {...}
	public ListitemRenderer getItemRenderer() {...}
	public void render(Listitem item, Object data) {...}
	public ListModel getSelectedModel() {...}

        /** Composer Implementation */

	public void doAfterCompose(Component comp) throws Exception {...}
	public ComponentInfo doBeforeCompose(Page page, Component parent,ComponentInfo compInfo) {...}
	public void doBeforeComposeChildren(Component comp) throws Exception {...}
	public boolean doCatch(Throwable ex) throws Exception {...}
	public void doFinally() throws Exception {...}

The org.zkoss.zkdemo.userguide.MainLayoutAPI interface is the main features of the demo.

public interface MainLayoutAPI {
	public Category[] getCategories();
	public ListModel getSelectedModel();
	public ListitemRenderer getItemRenderer();


This article shows the workings of the New ZK Demo and parts of ZK features we have used.
If you are interesting in other parts of ZKDemo, like "theme change", you can try: and

Finally, feel free to leave a comment to us.



  1. Composer : A way to compose a component's behavior in ZK, users can add "apply" to use it.
  2. FusionInvoker : Refer to the article An Introduction of ZK Composer
  3. forward : ZK's auto forward function from GenericForwardComposer, it' usage is here.
  4. GenericForwardComposer : See the introduction in this article : ZK MVC Made Easy

Feel free to leave a comment to us.

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