-
FEATURED COMPONENTS
First time here? Check out the FAQ!
I would like to create a grid completely in Java, but am struggling to find how to do this. I've read that it is possible to implement all zk code completely in Java, but I'm unsure what to put in an index.zul and grid.java file that will accomplish this.
Hello tylero,
At the following post you can find a pretty good example on how to dynamically create a listbox (Creating a grid is pretty similar, you just have to set the respective columns, and rows instead of listhead's, listitem's, and listcell's )
http://forum.zkoss.org/question/32343/programmatically-creation-of-the-list-box/
And some more examples:
http://forum.zkoss.org/question/86341/create-grid-columns-in-java-only/
http://forum.zkoss.org/question/77150/how-to-create-a-zk-grid-in-pure-java/
As for the index zul, i would put at least the window id, so that in the controller you will create the grid and then set as it's parent the window.
Best Regards,
Darksu
interesting post
/** * see: * books.zkoss.org/wiki/Small_Talks/2013/January/Building_User_Interface_Programmatically_with_Richlet * - put this source under WEB-INF/sources; * - compile LoadGrid.java using ZK library; * - put the LoadGrid.class generated in WEB-INF/classes * - put the following lines in WEB-INF/zk.xml:
* <richlet> * <richlet-name>LoadGrid</richlet-name> * <richlet-class>LoadGrid</richlet-class> * </richlet> * <richlet-mapping> * <richlet-name>LoadGrid</richlet-name> * <url-pattern>/LoadGrid</url-pattern> * </richlet-mapping> * <servlet-mapping>
* - Uncomment in WEB-INF/web.xml the following lines to enable richlet
* <servlet-mapping> * <servlet-name>zkLoader</servlet-name> * <url-pattern>/zk/*</url-pattern> * </servlet-mapping>
* - starts Tomcat; * - type in the Browser:
* http://<address>:8080/<your tomcat webapps HOME project>/zk/LoadGrid */ import java.util.*; import org.zkoss.zk.ui.Component; import org.zkoss.zk.ui.Desktop; import org.zkoss.zk.ui.Executions; import org.zkoss.zk.ui.GenericRichlet; import org.zkoss.zk.ui.Page; import org.zkoss.zul.Borderlayout; import org.zkoss.zul.*; import org.zkoss.zk.ui.event.EventListener; import org.zkoss.zk.ui.event.Event; import org.zkoss.zk.ui.event.Events; import org.zkoss.zul.impl.XulElement; import org.zkoss.zul.event.ListDataEvent; public class LoadGrid extends GenericRichlet { //Richlet// private static int numrighe = 56; private static int numcolonne = 10; String[][] model = new String[numrighe][numcolonne]; Grid gd; MyModel mm; int conta = 0; private class MyModel extends AbstractListModel implements ListModelExt { String[][] mymodel = new String[numrighe][numcolonne]; int nr; int nc; public MyModel(int nr, int nc) { mymodel = new String[nr][nc]; this.nr = nr; this.nc = nc; } public String getValueAt(int r, int c) { return mymodel[r][c]; } public Object getElementAt(int r) { return mymodel[r]; } public int getSize() { return nr; } public String getSortDirection(java.util.Comparator cmp) { return "natural"; } public void sort(java.util.Comparator cmp, boolean asc) { } public void fireTableRowsInserted(int start, int end) { System.out.println("====>FTROWSINSERTED ["+start+"] ["+end+"]"); fireEvent(ListDataEvent.INTERVAL_ADDED, start, end); } public void fireTableRowsDeleted(int start, int end) { System.out.println("====>FTROWSDELETED ["+start+"] ["+end+"]"); fireEvent(ListDataEvent.INTERVAL_REMOVED, start, end); } } public class MyRowRenderer implements RowRenderer { Vector righe = new Vector(); boolean vf; public MyRowRenderer (boolean vf) { this.vf = vf; for(int j = 0; j < model.length; ++j) righe.add(model[j]); } public void render(Row row, java.lang.Object data, int k) { String []datav = (String[])data; for (int i = 0; i < datav.length; i++) { Cell hb = null; Textbox tb = null; hb = new Cell(); tb = new Textbox(); tb.setText(new Integer(conta).toString() + "-" + new Integer(k).toString()+ "-" + new Integer(i).toString()); tb.setParent(hb); hb.setParent(row); if (vf) { hb.setHflex("1"); tb.setHflex("1"); } else { hb.setWidth("100%"); tb.setWidth("120px"); } tb.setHeight("30px"); hb.setHeight("30px"); row.setHeight("30px"); } } } public void service(Page p) { final Desktop dsk = Executions.getCurrent().getDesktop(); final Page page = p; Window w = null; int topwin = 30; w = new Window("LoadGrid", "overlapped", true); w.setHeight("600px"); w.setWidth("800px"); w.setStyle("position:absolute;border:solid 1px;"); w.setTop(new Integer(topwin)+"px"); w.setLeft("20px"); w.setPage(page); topwin += 20; Columns cls = null; Column c = null; gd = new Grid(); gd.setStyle("position:absolute;margin:4px;color:cyan;font-family:Gill Sans MT;font-size:14pt;"); gd.setLeft("50px"); gd.setTop("100px"); gd.setWidth("650px"); gd.setHeight("450px"); gd.setParent(w); cls = new Columns(); cls.setSizable(true); gd.appendChild(cls); for (int i = 0; i < numcolonne; i++) { c = new Column(); c.setLabel(new Integer(i).toString()); c.setParent(cls); } mm = new MyModel(numrighe, numcolonne); gd.setModel(mm); gd.setRowRenderer(new MyRowRenderer(false)); } }
Asked: 2015-06-25 14:33:37 +0800
Seen: 52 times
Last updated: Jun 26 '15