====== Adding JSF components programmatically ====== I've had to search a lot through internet in order to discover how to create JSF components programmaticaly. At the end, I didn't have to use it in the application I was creating, but I thought it worth to keep the research in some way. import javax.faces.application.Application; import javax.faces.component.html.*; import javax.faces.context.FacesContext; import com.rapidoaudit.model.AuditSkel; public class SectionsCollection { // rendered panel private HtmlPanelGrid grid = null; //private List sections; private int sectionsCounter; public borrado_20110603_SectionsCollection() { sectionsCounter = 0; //sections = new ArrayList(); } public void setComponent( HtmlPanelGrid newGrid ) { grid = newGrid; } public HtmlPanelGrid getComponent() { Application app = FacesContext.getCurrentInstance().getApplication(); grid = (HtmlPanelGrid) app.createComponent(HtmlPanelGrid.COMPONENT_TYPE); grid.setColumns(1); grid.setId("sections"); return grid; } public void addSection(AuditSkel skel) { Section s1; skel.addSection(); s1 = new Section(skel, sectionsCounter); grid.getChildren().add( s1.getGrid() ); sectionsCounter++; } // addSection } package com.rapidoaudit.view; import javax.el.ELContext; import javax.el.ValueExpression; import javax.faces.application.Application; import javax.faces.component.UIComponent; import javax.faces.component.UIInput; import javax.faces.component.UISelectItem; import javax.faces.component.html.HtmlInputText; import javax.faces.component.html.HtmlOutputLabel; import javax.faces.component.html.HtmlOutputText; import javax.faces.component.html.HtmlPanelGrid; import javax.faces.component.html.HtmlPanelGroup; import javax.faces.component.html.HtmlSelectOneMenu; import javax.faces.component.html.HtmlSelectBooleanCheckbox; import javax.faces.context.FacesContext; import javax.faces.convert.IntegerConverter; import com.rapidoaudit.model.AuditSkel; import java.util.Random; import org.primefaces.component.spinner.Spinner; public class Section { int sectNumber; // rendered panel private HtmlPanelGrid grid = null; public Section( AuditSkel newSkel, int newSectNumber ) { sectNumber = newSectNumber; } public Section() { Random rnd = new Random(); sectNumber = rnd.nextInt( 200000 ); } public Section( int intSectNumber ) { sectNumber = intSectNumber; } // for setting unique id's in the view private void compSetId( UIComponent comp, String id ) { comp.setId( id + new Integer(sectNumber).toString() ); } // for setting unique id's in the view @SuppressWarnings("unused") private void compSetId( UIComponent comp ) { // create a random stream of numbers (just for the sake of keep faces happy) Random rnd = new Random(); comp.setId( Integer.toString(rnd.nextInt(200000))); } /** * To set a value from an Expression Language expression. * Example: * * compSetValue( name_input, "#{auditSkel.name}" ); * * where "auditSkel" should be an existent bean * * @param comp * @param elExpression */ private void compSetValue( UIInput comp, String elExpression ) { ELContext elContext = FacesContext.getCurrentInstance().getELContext(); ValueExpression val1 = FacesContext.getCurrentInstance(). getApplication(). getExpressionFactory(). createValueExpression( elContext, elExpression, Object.class); comp.setValueExpression( "value", val1 ); } // compSetValue public HtmlPanelGrid getGrid( ) { if( grid == null ) { Application app = FacesContext.getCurrentInstance().getApplication(); grid = (HtmlPanelGrid) app.createComponent(HtmlPanelGrid.COMPONENT_TYPE); grid.setColumns(2); compSetId( grid, "Section" ); // HtmlOutputLabel name_label = (HtmlOutputLabel) app.createComponent(HtmlOutputLabel.COMPONENT_TYPE); name_label.setStyleClass("right"); name_label.setValue("Name"); name_label.setFor("type"); compSetId( name_label, "name_label" ); grid.getChildren().add(name_label); // HtmlInputText name_input = (HtmlInputText) app.createComponent(HtmlInputText.COMPONENT_TYPE); compSetId( name_input, "name_input" ); // aqui me quedo, no sé cómo se puede hacer esto //compSetValue( name_input, "#{auditSkel.getSection("+ sectNumber +").name}" ); compSetValue( name_input, "#{testProp.testObject(1).name}" ); grid.getChildren().add(name_input); // HtmlOutputLabel type_label = (HtmlOutputLabel) app.createComponent(HtmlOutputLabel.COMPONENT_TYPE); type_label.setStyleClass("right"); type_label.setValue("Type"); type_label.setFor("type"); compSetId( type_label, "type_label" ); grid.getChildren().add(type_label); // // // // // // // HtmlSelectOneMenu type_menu = (HtmlSelectOneMenu) app.createComponent(HtmlSelectOneMenu.COMPONENT_TYPE); compSetId( type_menu, "type_menu" ); type_menu.setValue("text_many"); UISelectItem type_item_1 = (UISelectItem) app.createComponent(UISelectItem.COMPONENT_TYPE); type_item_1.setItemLabel("Text (many lines)"); type_item_1.setItemValue("text_many"); compSetId( type_item_1, "type_item_1" ); type_menu.getChildren().add(type_item_1); UISelectItem type_item_2 = (UISelectItem) app.createComponent(UISelectItem.COMPONENT_TYPE); type_item_2.setItemLabel("Date"); type_item_2.setItemValue("date"); type_item_2.setId("type_item_2"); compSetId( type_item_2, "type_item_2" ); type_menu.getChildren().add(type_item_2); UISelectItem type_item_3 = (UISelectItem) app.createComponent(UISelectItem.COMPONENT_TYPE); type_item_3.setItemLabel("Text (one line)"); type_item_3.setItemValue("text_one"); compSetId( type_item_3, "type_item_3"); type_menu.getChildren().add(type_item_3); UISelectItem type_item_4 = (UISelectItem) app.createComponent(UISelectItem.COMPONENT_TYPE); type_item_4.setItemLabel("Document"); type_item_4.setItemValue("file"); compSetId( type_item_4, "type_item_4"); type_menu.getChildren().add(type_item_4); UISelectItem type_item_5 = (UISelectItem) app.createComponent(UISelectItem.COMPONENT_TYPE); type_item_5.setItemLabel("Image"); type_item_5.setItemValue("image"); compSetId( type_item_5, "type_item_5"); type_menu.getChildren().add(type_item_5); grid.getChildren().add(type_menu); // HtmlOutputLabel goesReportLabel = (HtmlOutputLabel) app.createComponent(HtmlOutputLabel.COMPONENT_TYPE); goesReportLabel.setStyleClass("right"); goesReportLabel.setValue("Does this section appear on the report?"); goesReportLabel.setFor("type"); compSetId( goesReportLabel, "goesReportLabel" ); grid.getChildren().add(goesReportLabel); // HtmlSelectBooleanCheckbox goesReport = (HtmlSelectBooleanCheckbox) app.createComponent(HtmlSelectBooleanCheckbox.COMPONENT_TYPE); compSetId( goesReport, "goesReport" ); grid.getChildren().add(goesReport); // HtmlOutputLabel isFindingLabel = (HtmlOutputLabel) app.createComponent(HtmlOutputLabel.COMPONENT_TYPE); isFindingLabel.setStyleClass("right"); isFindingLabel.setValue("Is this section part of a finding?"); isFindingLabel.setFor("isFinding" ); compSetId( isFindingLabel, "isFindingLabel" ); grid.getChildren().add(isFindingLabel); // HtmlSelectBooleanCheckbox isFinding = (HtmlSelectBooleanCheckbox) app.createComponent(HtmlSelectBooleanCheckbox.COMPONENT_TYPE); compSetId( isFinding, "isFinding" ); grid.getChildren().add(isFinding); // HtmlOutputText minimumMaximum = (HtmlOutputText) app.createComponent( HtmlOutputText.COMPONENT_TYPE ); compSetId( minimumMaximum, "minimumMaximum" ); minimumMaximum.setValue("In each audit work this will appear..."); minimumMaximum.setStyleClass("right"); grid.getChildren().add(minimumMaximum); HtmlOutputLabel empty1 = (HtmlOutputLabel) app.createComponent( HtmlOutputLabel.COMPONENT_TYPE ); compSetId( empty1, "empty1" ); empty1.setValue(""); grid.getChildren().add(empty1); // // HtmlOutputLabel aMinimumOf = (HtmlOutputLabel) app.createComponent(HtmlOutputLabel.COMPONENT_TYPE); compSetId(aMinimumOf, "aMinimumOf"); aMinimumOf.setValue("A minimum of" ); aMinimumOf.setStyleClass("right"); aMinimumOf.setFor("panelMin"); grid.getChildren().add(aMinimumOf); // HtmlPanelGroup panelMin = (HtmlPanelGroup) app.createComponent(HtmlPanelGroup.COMPONENT_TYPE); compSetId(panelMin, "panelMin"); // Spinner spMin = (Spinner) app.createComponent( Spinner.COMPONENT_TYPE ); compSetId( spMin, "spMin" ); spMin.setValue(1); spMin.setMin(0); spMin.setSize(5); spMin.setMax(1000); spMin.setConverter(new IntegerConverter()); panelMin.getChildren().add(spMin); // HtmlOutputText times = (HtmlOutputText) app.createComponent(HtmlOutputText.COMPONENT_TYPE); compSetId( times, "times" ); times.setValue("times"); panelMin.getChildren().add(times); // grid.getChildren().add(panelMin); // HtmlOutputLabel aMaximumOf = (HtmlOutputLabel) app.createComponent(HtmlOutputLabel.COMPONENT_TYPE); compSetId(aMaximumOf, "aMaximumOf"); aMaximumOf.setValue("A maximum of" ); aMaximumOf.setStyleClass("right"); aMaximumOf.setFor("panelMin"); grid.getChildren().add(aMaximumOf); // HtmlPanelGroup panelMax = (HtmlPanelGroup) app.createComponent(HtmlPanelGroup.COMPONENT_TYPE); compSetId(panelMax, "panelMax"); // Spinner spMax = (Spinner) app.createComponent( Spinner.COMPONENT_TYPE ); compSetId( spMax, "spMax" ); spMax.setValue(1); spMax.setMin(0); spMax.setSize(5); spMax.setMax(1000); spMax.setValue(0); spMax.setConverter(new IntegerConverter()); panelMax.getChildren().add(spMax); // HtmlOutputText times2 = (HtmlOutputText) app.createComponent(HtmlOutputText.COMPONENT_TYPE); compSetId( times2, "times2" ); times2.setValue("times"); panelMax.getChildren().add(times2); // grid.getChildren().add(panelMax); // } return grid; } } ~~DISQUS~~