Friday, August 24, 2007

Creating Dynamic JSF Components

Creating Dynamic Components at JSF is really easy but because of performance problems and the large number of components to generate i choose different way at project. A simple sample:

Use datagrid/datatable object to add your components:

<h:panelgrid id="grid1" styleclass="panelGrid" columns="2">< /h:panelgrid>
<hx:commandexbutton id="newComponentBtn" action="#{pc_DynaComponentView.addNewComponent}" styleclass="commandExButton" type="submit" value="New Component"></hx:commandexbutton>

Sample JSF:

<h:form id="form1" styleclass="form">
<h:panelgrid id="grid1" styleclass="panelGrid" columns="2"></h:panelgrid>
<hx:commandexbutton id="newComponentBtn" action="#{pc_PageView.addNewComponent}" styleclass="commandExButton" type="submit" value="New Component"></hx:commandexbutton>
</h:form>

Sample Code at Backing Bean:

public void addNewComponent() {
   // create sample components
   HtmlSelectOneListbox listbox = new HtmlSelectOneListbox();
   HtmlInputText text1 = new HtmlInputText();
   text1.setValue("TEST");
   List valueList = new ArrayList();
   SelectItem selectItem = new SelectItem("TEST1", "TEST1");
   valueList.add(selectItem);
   selectItem = new SelectItem("TEST2", "TEST2");
   valueList.add(selectItem);
   UISelectItems items = new UISelectItems();
   items.setValue(valueList);
   listbox.getChildren().add(items);
   grid1 = getGrid1();
   // Add components
   grid1.getChildren().add(listbox);
   grid1.getChildren().add(text1);

}