java:addingcomponentsprogramatically
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| java:addingcomponentsprogramatically [2013/03/08 23:13] – rlunaro | java:addingcomponentsprogramatically [2022/12/02 21:02] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== 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. | ||
| + | |||
| + | <code java> | ||
| + | |||
| + | |||
| + | 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< | ||
| + | private int sectionsCounter; | ||
| + | |||
| + | public borrado_20110603_SectionsCollection() | ||
| + | { | ||
| + | sectionsCounter = 0; | ||
| + | // | ||
| + | } | ||
| + | |||
| + | 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(" | ||
| + | |||
| + | return grid; | ||
| + | } | ||
| + | |||
| + | public void addSection(AuditSkel skel) | ||
| + | { | ||
| + | |||
| + | Section s1; | ||
| + | |||
| + | skel.addSection(); | ||
| + | |||
| + | s1 = new Section(skel, | ||
| + | |||
| + | grid.getChildren().add( s1.getGrid() ); | ||
| + | |||
| + | sectionsCounter++; | ||
| + | |||
| + | } // addSection | ||
| + | |||
| + | } | ||
| + | |||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | <code java> | ||
| + | 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(" | ||
| + | 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, "# | ||
| + | * | ||
| + | * where " | ||
| + | * | ||
| + | * @param comp | ||
| + | * @param elExpression | ||
| + | */ | ||
| + | private void compSetValue( UIInput comp, String elExpression ) | ||
| + | { | ||
| + | ELContext elContext = FacesContext.getCurrentInstance().getELContext(); | ||
| + | |||
| + | ValueExpression val1 = FacesContext.getCurrentInstance(). | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | comp.setValueExpression( " | ||
| + | |||
| + | } // compSetValue | ||
| + | |||
| + | |||
| + | public HtmlPanelGrid getGrid( ) | ||
| + | { | ||
| + | if( grid == null ) | ||
| + | { | ||
| + | Application app = FacesContext.getCurrentInstance().getApplication(); | ||
| + | |||
| + | grid = (HtmlPanelGrid) app.createComponent(HtmlPanelGrid.COMPONENT_TYPE); | ||
| + | |||
| + | grid.setColumns(2); | ||
| + | compSetId( grid, " | ||
| + | |||
| + | // < | ||
| + | HtmlOutputLabel name_label = (HtmlOutputLabel) app.createComponent(HtmlOutputLabel.COMPONENT_TYPE); | ||
| + | name_label.setStyleClass(" | ||
| + | name_label.setValue(" | ||
| + | name_label.setFor(" | ||
| + | compSetId( name_label, " | ||
| + | grid.getChildren().add(name_label); | ||
| + | |||
| + | // < | ||
| + | HtmlInputText name_input = (HtmlInputText) app.createComponent(HtmlInputText.COMPONENT_TYPE); | ||
| + | compSetId( name_input, " | ||
| + | // aqui me quedo, no sé cómo se puede hacer esto | ||
| + | // | ||
| + | compSetValue( name_input, "# | ||
| + | grid.getChildren().add(name_input); | ||
| + | |||
| + | //< | ||
| + | HtmlOutputLabel type_label = (HtmlOutputLabel) app.createComponent(HtmlOutputLabel.COMPONENT_TYPE); | ||
| + | type_label.setStyleClass(" | ||
| + | type_label.setValue(" | ||
| + | type_label.setFor(" | ||
| + | compSetId( type_label, " | ||
| + | grid.getChildren().add(type_label); | ||
| + | |||
| + | //< | ||
| + | // | ||
| + | // | ||
| + | // | ||
| + | // | ||
| + | // | ||
| + | //</ | ||
| + | HtmlSelectOneMenu type_menu = (HtmlSelectOneMenu) app.createComponent(HtmlSelectOneMenu.COMPONENT_TYPE); | ||
| + | compSetId( type_menu, " | ||
| + | type_menu.setValue(" | ||
| + | |||
| + | UISelectItem type_item_1 = (UISelectItem) app.createComponent(UISelectItem.COMPONENT_TYPE); | ||
| + | type_item_1.setItemLabel(" | ||
| + | type_item_1.setItemValue(" | ||
| + | compSetId( type_item_1, | ||
| + | type_menu.getChildren().add(type_item_1); | ||
| + | |||
| + | UISelectItem type_item_2 = (UISelectItem) app.createComponent(UISelectItem.COMPONENT_TYPE); | ||
| + | type_item_2.setItemLabel(" | ||
| + | type_item_2.setItemValue(" | ||
| + | type_item_2.setId(" | ||
| + | compSetId( type_item_2, | ||
| + | type_menu.getChildren().add(type_item_2); | ||
| + | |||
| + | UISelectItem type_item_3 = (UISelectItem) app.createComponent(UISelectItem.COMPONENT_TYPE); | ||
| + | type_item_3.setItemLabel(" | ||
| + | type_item_3.setItemValue(" | ||
| + | compSetId( type_item_3, | ||
| + | type_menu.getChildren().add(type_item_3); | ||
| + | |||
| + | UISelectItem type_item_4 = (UISelectItem) app.createComponent(UISelectItem.COMPONENT_TYPE); | ||
| + | type_item_4.setItemLabel(" | ||
| + | type_item_4.setItemValue(" | ||
| + | compSetId( type_item_4, | ||
| + | type_menu.getChildren().add(type_item_4); | ||
| + | |||
| + | UISelectItem type_item_5 = (UISelectItem) app.createComponent(UISelectItem.COMPONENT_TYPE); | ||
| + | type_item_5.setItemLabel(" | ||
| + | type_item_5.setItemValue(" | ||
| + | compSetId( 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(" | ||
| + | goesReportLabel.setValue(" | ||
| + | goesReportLabel.setFor(" | ||
| + | compSetId( goesReportLabel, | ||
| + | grid.getChildren().add(goesReportLabel); | ||
| + | |||
| + | //< | ||
| + | HtmlSelectBooleanCheckbox goesReport = (HtmlSelectBooleanCheckbox) app.createComponent(HtmlSelectBooleanCheckbox.COMPONENT_TYPE); | ||
| + | compSetId( goesReport, " | ||
| + | grid.getChildren().add(goesReport); | ||
| + | |||
| + | //< | ||
| + | HtmlOutputLabel isFindingLabel = (HtmlOutputLabel) app.createComponent(HtmlOutputLabel.COMPONENT_TYPE); | ||
| + | isFindingLabel.setStyleClass(" | ||
| + | isFindingLabel.setValue(" | ||
| + | isFindingLabel.setFor(" | ||
| + | compSetId( isFindingLabel, | ||
| + | grid.getChildren().add(isFindingLabel); | ||
| + | |||
| + | //< | ||
| + | HtmlSelectBooleanCheckbox isFinding = (HtmlSelectBooleanCheckbox) app.createComponent(HtmlSelectBooleanCheckbox.COMPONENT_TYPE); | ||
| + | compSetId( isFinding, " | ||
| + | grid.getChildren().add(isFinding); | ||
| + | |||
| + | //< | ||
| + | HtmlOutputText minimumMaximum = (HtmlOutputText) app.createComponent( HtmlOutputText.COMPONENT_TYPE ); | ||
| + | compSetId( minimumMaximum, | ||
| + | minimumMaximum.setValue(" | ||
| + | minimumMaximum.setStyleClass(" | ||
| + | grid.getChildren().add(minimumMaximum); | ||
| + | |||
| + | HtmlOutputLabel empty1 = (HtmlOutputLabel) app.createComponent( HtmlOutputLabel.COMPONENT_TYPE ); | ||
| + | compSetId( empty1, " | ||
| + | empty1.setValue("" | ||
| + | grid.getChildren().add(empty1); | ||
| + | |||
| + | //< | ||
| + | |||
| + | //< | ||
| + | HtmlOutputLabel aMinimumOf = (HtmlOutputLabel) app.createComponent(HtmlOutputLabel.COMPONENT_TYPE); | ||
| + | compSetId(aMinimumOf, | ||
| + | aMinimumOf.setValue(" | ||
| + | aMinimumOf.setStyleClass(" | ||
| + | aMinimumOf.setFor(" | ||
| + | grid.getChildren().add(aMinimumOf); | ||
| + | |||
| + | //< | ||
| + | HtmlPanelGroup panelMin = (HtmlPanelGroup) app.createComponent(HtmlPanelGroup.COMPONENT_TYPE); | ||
| + | compSetId(panelMin, | ||
| + | |||
| + | // | ||
| + | Spinner spMin = (Spinner) app.createComponent( Spinner.COMPONENT_TYPE ); | ||
| + | compSetId( 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.setValue(" | ||
| + | panelMin.getChildren().add(times); | ||
| + | |||
| + | //</ | ||
| + | grid.getChildren().add(panelMin); | ||
| + | |||
| + | |||
| + | //< | ||
| + | HtmlOutputLabel aMaximumOf = (HtmlOutputLabel) app.createComponent(HtmlOutputLabel.COMPONENT_TYPE); | ||
| + | compSetId(aMaximumOf, | ||
| + | aMaximumOf.setValue(" | ||
| + | aMaximumOf.setStyleClass(" | ||
| + | aMaximumOf.setFor(" | ||
| + | grid.getChildren().add(aMaximumOf); | ||
| + | |||
| + | //< | ||
| + | HtmlPanelGroup panelMax = (HtmlPanelGroup) app.createComponent(HtmlPanelGroup.COMPONENT_TYPE); | ||
| + | compSetId(panelMax, | ||
| + | |||
| + | // | ||
| + | Spinner spMax = (Spinner) app.createComponent( Spinner.COMPONENT_TYPE ); | ||
| + | compSetId( 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.setValue(" | ||
| + | panelMax.getChildren().add(times2); | ||
| + | |||
| + | // | ||
| + | grid.getChildren().add(panelMax); | ||
| + | |||
| + | //</ | ||
| + | |||
| + | } | ||
| + | |||
| + | return grid; | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | ~~DISQUS~~ | ||
| + | |||
| + | |||
