Saturday, 11 January 2014

ZK MVVM List box with check box – Simple Example

ZK Version : ZK 7


Introduction of MVVM

List Box Template
ZK List box Documentation

Project Structure



<window title="Example for ZK MVVM List Box" width="500px"
viewModel="@id('vm') @init('zkexamples.mvvm.ListBox2')">
<label value="You are using: ${desktop.webApp.version}" />
<listbox model="@load(vm.allPersons)" checkmark="true"
multiple="true" selectedItem="@bind(vm.selectedPerson)"
<listhead sizable="true">
<listheader label="First Name" sortDirection="ascending"
sort="auto(firstName)" />
<listheader label="Last Name" sort="auto(lastName)" />
<listheader label="Email" sort="auto(email)" />
<template name="model" var="p1">
<listcell label="@load(p1.firstName)" />
<listcell label="@load(p1.lastName)" />
<listcell label="@load(" />
<button label="Show Selected Items email id"

package zkexamples.mvvm;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.zkoss.bind.annotation.Command;
import org.zkoss.zul.Messagebox;

public class ListBox2 {

private List<person> allPersons = new ArrayList<person>();
private person selectedPerson;
private List<person> selectedPersons;

public person getSelectedPerson() {
return selectedPerson;

public void setSelectedPerson(person selectedPerson) {
this.selectedPerson = selectedPerson;

public List<person> getSelectedPersons() {
return selectedPersons;

public void setSelectedPersons(List<person> selectedPersons) {
this.selectedPersons = selectedPersons;

public List<person> getAllPersons() {
return allPersons;

public void setAllPersons(List<person> allPersons) {
this.allPersons = allPersons;

public ListBox2() {

allPersons.add(new person("John", "James", ""));
allPersons.add(new person("Taylor", "Harris", ""));
allPersons.add(new person("Allen", "Scott", ""));


public void onShowEmailID()
String emailIDs="";

if (this.selectedPersons==null)
{" No Items selected");

if (this.selectedPersons.size()==0)
{" No Items selected");

for (Iterator<person> i = selectedPersons.iterator(); i
.hasNext();) {
person tmp =;
emailIDs = emailIDs.concat(tmp.getEmail()).concat(";");
}"Selected Emails are " + emailIDs);

// inner class
public class person {
private String firstName;
private String lastName;
private String email;

public person(String firstName, String lastName, String email) {
this.firstName = firstName;
this.lastName = lastName; = email;

public String getFirstName() {
return firstName;

public void setFirstName(String firstName) {
this.firstName = firstName;

public String getLastName() {
return lastName;

public void setLastName(String lastName) {
this.lastName = lastName;

public String getEmail() {
return email;

public void setEmail(String email) { = email;





  1. I can't find selectedItems in listbox..

  2. What a great Tutorial! For those who want to use multiple selection with ListModelList, you will need to enable multi-selection it using setMultiple(true) just after initialization to get thing work.


  3. how to get a single selecteditem, even using checkmark ="true" in listbox


Passing Parameter between two files using MVVM

This examples shows how to pass parameter between two zul screens. In this example, we are passing some parameters from the parent vm to...