zk动态产生多个页面的例子代码:
/** * * @功能 添加参数 * @author 创建人 gao_jie * @date 创建日期 Apr 23, 2009 * @version 1.0 * */ public class AddParameter extends Window implements AfterCompose { private static final long serialVersionUID = 1L; private int num = 0;// 条件个数 private Window win;// 参数风格 private Rows parameterRows;// 条件行数 private DesignerWnd desiWnd = null; @SuppressWarnings("unused") private Spreadsheet spreadSheet; private InforReport inforreport; /* * (non-Javadoc) * * @see org.zkoss.zk.ui.ext.AfterCompose#afterCompose() */ public void afterCompose() { parameterRows = (Rows) this.getFellow("parameterRows"); win = (Window) this.getFellow("AddParameter"); desiWnd = (DesignerWnd) Sessions.getCurrent().getAttribute("dWnd"); spreadSheet = (Spreadsheet) desiWnd.getFellow("spreadSheet"); inforreport = spreadSheet.getReport(); this.initiPage(inforreport.getParameters()); } /** * 初始化参数页面 获取已定义的参数 现实参数的name,type,defaultValue并可改 * * @param Parameters */ @SuppressWarnings("unchecked") public void initiPage(Map Parameters) { parameterRows.getChildren().clear(); if (Parameters != null && !Parameters.isEmpty()) { // 遍历Parameters的键集 Set parametset = Parameters.keySet(); Iterator ir = parametset.iterator(); while (ir.hasNext()) { String key = (String) ir.next(); // 获取已有的每一个参数 Parameter parameter = (Parameter) Parameters.get(key); // 计数器+1 num = num + 1; // 为每个参数在parameterRows里创建一个Row Row row = new Row(); row.setId("row_" + num); row.setParent(parameterRows); // 每个Row添加一个name,赋值为当前获取参数的name Textbox name = new Textbox(); name.setId("name_" + num); name.setWidth("80px"); name.setValue(parameter.getName()); name.setParent(row); // 每个Row添加一个type,赋值为当前获取参数的type Listbox type = new Listbox(); type.setParent(row); type.setId("type_" + num); type.setMold("select"); type.appendChild(new Listitem("字符串|String", "String")); type.appendChild(new Listitem("整型|Int", "Int")); type.appendChild(new Listitem("日期|Date", "Date")); type.appendChild(new Listitem("长整型|Long", "Long")); type.appendChild(new Listitem("单精度|Single", "Single")); type.appendChild(new Listitem("双精度|Double", "Double")); type.appendChild(new Listitem("货币|Currency", "Currency")); type.appendChild(new Listitem("字节|Byte", "Byte")); type.appendChild(new Listitem("布尔|Boolean", "Boolean")); type.appendChild(new Listitem("时间|Time", "Time")); type.appendChild(new Listitem("日期时间|Datetime", "Datetime")); if (parameter.getType().contains("String")) { type.setSelectedIndex(0); } else if (parameter.getType().contains("Int")) { type.setSelectedIndex(1); } else if (parameter.getType().contains("Date")) { type.setSelectedIndex(2); } else if (parameter.getType().contains("Long")) { type.setSelectedIndex(3); } else if (parameter.getType().contains("Single")) { type.setSelectedIndex(4); } else if (parameter.getType().contains("Double")) { type.setSelectedIndex(5); } else if (parameter.getType().contains("Currency")) { type.setSelectedIndex(6); } else if (parameter.getType().contains("Byte")) { type.setSelectedIndex(7); } else if (parameter.getType().contains("Boolean")) { type.setSelectedIndex(8); } else if (parameter.getType().contains("Time")) { type.setSelectedIndex(9); } else if (parameter.getType().contains("Datetime")) { type.setSelectedIndex(10); } else { type.setSelectedIndex(0); } // 每个Row添加一个defaultValue,赋值为当前获取参数的defaultValue Textbox defaultValue = new Textbox(); defaultValue.setId("parameter_" + num); defaultValue.setWidth("80px"); defaultValue.setValue(parameter.getDefaultValue()); defaultValue.setParent(row); // 每个Row添加一个"删除"按钮,触发删除事件 Button delbtn = new Button(); delbtn.setId("delbtd_" + num); delbtn.setLabel("删除"); delbtn.addEventListener("onClick", new DelEventListener(num)); delbtn.setParent(row); } } } /** * 添加参数 */ public void addParameter() { num = num + 1; // 产生Row Row row = new Row(); row.setId("row_" + num); row.setParent(parameterRows); Textbox name = new Textbox(); name.setId("name_" + num); name.setWidth("80px"); name.setParent(row); Listbox type = new Listbox(); type.setParent(row); type.setId("type_" + num); type.setMold("select"); type.appendChild(new Listitem("字符串|String", "String")); type.appendChild(new Listitem("整型|Int", "Int")); type.appendChild(new Listitem("日期|Date", "Date")); type.appendChild(new Listitem("长整型|Long", "Long")); type.appendChild(new Listitem("单精度|Single", "Single")); type.appendChild(new Listitem("双精度|Double", "Double")); type.appendChild(new Listitem("货币|Currency", "Currency")); type.appendChild(new Listitem("字节|Byte", "Byte")); type.appendChild(new Listitem("布尔|Boolean", "Boolean")); type.appendChild(new Listitem("时间|Time", "Time")); type.appendChild(new Listitem("日期时间|Datetime", "Datetime")); type.setSelectedIndex(0); Textbox defaultValue = new Textbox(); defaultValue.setId("parameter_" + num); defaultValue.setWidth("80px"); defaultValue.setParent(row); Button delbtn = new Button(); delbtn.setId("delbtd_" + num); delbtn.setLabel("删除"); delbtn.addEventListener("onClick", new DelEventListener(num)); delbtn.setParent(row); } /** * * @功能 监听事件 "删除参数" * */ public class DelEventListener implements EventListener { int no; public DelEventListener(int no) { this.no = no; } @SuppressWarnings("unchecked") public void onEvent(Event arg0) throws Exception { // 移除才操作 win.getFellow("row_" + no).detach(); // 改变现有的,该行以后的所有行,上移一位 for (int i = no + 1; i <= num; i++) { int j = i - 1; win.getFellow("row_" + i).setId("row_" + j); win.getFellow("name_" + i).setId("name_" + j); win.getFellow("type_" + i).setId("type_" + j); win.getFellow("parameter_" + i).setId("parameter_" + j); Button bun = (Button) win.getFellow("delbtd_" + i); Iterator ite = bun.getListenerIterator("onClick"); bun.removeEventListener("onClick", (EventListener) ite.next()); bun.setId("delbtd_" + j); bun.addEventListener("onClick", new DelEventListener(j)); } num--; } } /** * 保存选择 */ @SuppressWarnings("unchecked") public void saveParameter() { Map map = new HashMap(); // 定位到待保存的每一行 for (int i = 1; i <= num; i++) { // 获取页面的输入值 String name = ((Textbox) this.getFellow("name_" + i)).getValue() .trim(); String type = (String) ((Listbox) this.getFellow("type_" + i)) .getSelectedItem().getValue(); String defaultValue = ((Textbox) this.getFellow("parameter_" + i)) .getValue(); // 当name不为空且不重复,则把页面获取的数据存入Parameter对象 if (!"".equals(name) && !map.containsKey(name)) { Parameter tempparameter = new Parameter(); tempparameter.setName(name); tempparameter.setType(type); tempparameter.setDefaultValue(defaultValue); // 再以name为键Parameter对象为值,存入一个Map对象 map.put(name, tempparameter); } else if (map.containsKey(name)) { Message.showInfo("第" + i + "行参数名已存在"); return; } else { Message.showInfo("第" + i + "行参数名不能为空"); return; } } // 把已存的map作为参数列与report关联,并初始化页面 num = 0; inforreport.setParameters(map); this.initiPage(inforreport.getParameters()); Message.showInfo("保存成功!"); } }
