Monday, March 4, 2013

Put Listbox inside list Item in zk using Children



Demotest.zul

<zk>
      <window width="1050px" height="380px"
            apply="org.zkoss.bind.BindComposer"
            viewModel="@id('vm') @init('com.test.DemoTest')" vflex="true"
            hflex="true">

            <vlayout sclass="vertical-scroll" vflex="1">
                  <groupbox children="@load(vm.listBox)">
                        <template name="children" var="listbox">
                              <listbox sizedByContent="true"
                                    sclass="vertical-scroll" checkmark="true" multiple="true"
                                    model="@load(listbox.listModel)"
                                    itemRenderer="@bind(listbox.render)"
                                    selectedItems="@bind(listbox.selectedList)">
                                    <listhead children="@load(listbox.headerList)"
                                          sizable="false">
                                          <template name="children"
                                                var="headerName">
                                                <listheader
                                                      label="@load(headerName.name)"
                                                      sort="@load(headerName.value)">
                                                </listheader>
                                          </template>
                                    </listhead>
                              </listbox>
                        </template>
                  </groupbox>

            </vlayout>

      </window>
</zk>

DemoTest.java
package com.test;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.zkoss.bind.annotation.AfterCompose;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.Component;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;

public class DemoTest {

                /**
                 * @author Himanshu
                 */

                List<Object> listBox = new ArrayList<Object>();

                public List<Object> getListBox() {
                               
                                return listBox;
                }

                public void setListBox(List<Object> listBox) {
                                this.listBox = listBox;
                }

                @AfterCompose
                public void afterCompose(@ContextParam(ContextType.VIEW) Component view) {
                                ChilderList chlist = new ChilderList();
                                chlist.setHeaderList(getHeaderList());
                                chlist.setListModel(getModeList());
                                chlist.setSelectedList(getSelectedData());
                                chlist.setRender(getItemRenderer());
                                listBox.add(chlist);
                                chlist = new ChilderList();
                                chlist.setHeaderList(getHeaderList());
                                chlist.setListModel(getModeList());
                                chlist.setSelectedList(getSelectedData());
                                chlist.setRender(getItemRenderer());
                                listBox.add(chlist);
                                chlist = new ChilderList();
                                chlist.setHeaderList(getHeaderList());
                                chlist.setListModel(getModeList());
                                chlist.setSelectedList(getSelectedData());
                                chlist.setRender(getItemRenderer());
                                listBox.add(chlist);
                }

                @SuppressWarnings("unchecked")
                @NotifyChange("*")
                public ListitemRenderer getItemRenderer() {

                                ListitemRenderer _rowRenderer = null;
                                if (_rowRenderer == null) {
                                                _rowRenderer = new ListitemRenderer() {
                                                                public void render(Listitem row, Object data, int index)
                                                                                                throws Exception {
                                                                                Data dataBean = (Data) data;
                                                                                row.setAttribute("data", dataBean);
                                                                                row.appendChild(new Listcell(dataBean.getName()));
                                                                                row.appendChild(new Listcell(dataBean.getAdress()));
                                                                                row.appendChild(new Listcell(String.valueOf(dataBean
                                                                                                                .getPhone())));
                                                                }

                                                };
                                }
                                return _rowRenderer;
                }

                public List<ListModelList<Data>> getMultipleList() {
                                List<ListModelList<Data>> mList = new ArrayList<ListModelList<Data>>();
                                mList.add(getModeList());
                                mList.add(getModeList());
                                return mList;
                }

                public ListModelList<Data> getModeList() {

                                ListModelList<Data> mdataList = new ListModelList<Data>();
                                Data d = new Data();
                                d.name = "Himanshu";
                                d.adress = "Delhi";
                                d.phone = 90157987;
                                mdataList.add(d);
                                d = new Data();
                                d.name = "Subodh";
                                d.adress = "UK";
                                d.phone = 348;
                                mdataList.add(d);
                                d = new Data();
                                d.name = "Mukesh";
                                d.adress = "Bihar";
                                d.phone = 35;
                                mdataList.add(d);
                                d = new Data();
                                d.name = "Shrawan";
                                d.adress = "Up";
                                d.phone = 1;
                                mdataList.add(d);
                                mdataList.setMultiple(true);
                                return mdataList;
                }


                public class Data {
                                String name;

                                public String getAdress() {
                                                return adress;
                                }

                                public void setAdress(String adress) {
                                                this.adress = adress;
                                }

                                public Integer getPhone() {
                                                return phone;
                                }

                                public void setPhone(Integer phone) {
                                                this.phone = phone;
                                }

                                String adress;
                                Integer phone;

                                public String getName() {
                                                return name;
                                }

                                public void setName(String name) {
                                                this.name = name;
                                }

                }

                public ListModelList<HeadData> getHeaderList() {
                                ListModelList<HeadData> hlist = new ListModelList<HeadData>();
                                HeadData hd = new HeadData();
                                hd.setName("Name");
                                hd.setValue("auto(name)");
                                hlist.add(hd);
                                hd = new HeadData();
                                hd.setName("Address");
                                hd.setValue("auto(adress)");
                                hlist.add(hd);
                                ;
                                hd = new HeadData();
                                hd.setName("Phone");
                                hd.setValue("auto(phone)");
                                hlist.add(hd);
                                return hlist;
                }

                public Set getSelectedData() {
                                Set set = null;
                                return set;
                }

                public class HeadData {
                                String name;

                                public String getName() {
                                                return name;
                                }

                                public void setName(String name) {
                                                this.name = name;
                                }

                                public String getValue() {
                                                return value;
                                }

                                public void setValue(String value) {
                                                this.value = value;
                                }

                                String value;
                }

                public class ChilderList {
                                List<ListModelList<Data>> list;

                                public List<ListModelList<Data>> getList() {
                                                return list;
                                }

                                public void setList(List<ListModelList<Data>> list) {
                                                this.list = list;
                                }

                                public ListitemRenderer getRender() {
                                                return render;
                                }

                                public void setRender(ListitemRenderer render) {
                                                this.render = render;
                                }

                                public Set getSelectedList() {
                                                return selectedList;
                                }

                                public void setSelectedList(Set selectedList) {
                                                this.selectedList = selectedList;
                                }

                                public ListModelList<HeadData> getHeaderList() {
                                                return headerList;
                                }

                                public void setHeaderList(ListModelList<HeadData> headerList) {
                                                this.headerList = headerList;
                                }

                                ListModelList<Data> listModel;

                                public ListModelList<Data> getListModel() {
                                                return listModel;
                                }

                                public void setListModel(ListModelList<Data> listModel) {
                                                this.listModel = listModel;
                                }

                                ListitemRenderer render;
                                Set selectedList;
                                ListModelList<HeadData> headerList;

                }

}

Java Quick Program

1. Write a Code to generate Random numbers public class GenerateRandomNumbers { public static void main(String[] args) { System.out...