adding new UIComponents to ViewRoot
Hi people...
I have a button on my JSP Page:
<h:commandButton id="B_1_1" value="click me" actionListener="#{myBean.addComponent}" />
in the add Component method i want to add now a new command button to the view and make it display.
public void addComponent(ActionEvent e){
UICommand c = new UICommand();
c.setId("test");
c.setValue("test1");
c.setRendered(true);
FacesContext.getCurrentInstance().getViewRoot().findComponent("myForm").getChildren().add(c);
}
i can see in debug mode that the component was added ... but on the page i cant see it...
can anyone help ?
thx
# 3
Relax ..
Apparently I don't have an JSF environment here at this PC to test your code snippet in.
But here is the way how I should do this and this should work:
JSF<h:form binding="#{myBean.form}">
<h:commandButton value="add" action="#{myBean.addCommandButton}" />
</h:form>
MyBeanprivate HtmlForm form; // + getter + setter;
public void addCommandButton() {
HtmlCommandButton button = new HtmlCommandButton();
button.setValue("test");
form.getChildren().add(button);
}
Be specific in declaring components. UICommand can also be HtmlCommandLink. I guess this was causing the problem.
# 4
> Relax ..
I wish I could... and thanks for the answer ... but unfortunately it still doesnt work ...
> JSF<h:form binding="#{myBean.form}">
> <h:commandButton value="add"
> action="#{myBean.addCommandButton}" />
> /h:form>
the binding of the form to the backing bean pretty much just simulates:
FacesContext.getCurrentInstance().getViewRoot().findComponent("myForm");
> Be specific in declaring components. UICommand can
> also be HtmlCommandLink. I guess this was causing the
> problem.
i tried UICommand, HtmlCommandButton, tried to intialize it over the constructor... tried to create a HtmlCommandButton over the FacesContext.getCurrentInstance().getApplication().createComponent(HtmlCommandButton.COMPONENT_TYPE)
i setRendered to true... to false and tried to render it later ... nothing works... nothing :(
# 5
protected void populatePanel() {
FacesContext facesContext = FacesContext.getCurrentInstance();
Application application = facesContext.getApplication();
panel = (UIPanel) application.createComponent(
"javax.faces.HtmlPanelGrid");
for (int index = 0; index < this.fieldNames.length; index++) {
UIOutput output = (UIOutput) application.createComponent(
"javax.faces.HtmlOutputLabel");
output.setValue(fieldNames[index]);
output.getAttributes().put("for", fieldNames[index]);
UIMessage message = (UIMessage) application.createComponent(
"javax.faces.HtmlMessage");
message.getAttributes().put("for", fieldNames[index]);
String valueBindingExpression = "#{" + entityName + "CRUD.entity." +
fieldNames[index] + "}";
System.out.println(valueBindingExpression);
ValueBinding valueBinding = application.createValueBinding(valueBindingExpression);
UIInput input = (UIInput) application.createComponent("javax.faces.HtmlInputText");
input.setValueBinding("value", valueBinding);
input.setId(fieldNames[index]);
input.setRequired(true);
panel.getChildren().add(output);
panel.getChildren().add(input);
panel.getChildren().add(message);
}
Or you can bind HtmlPanelGrid to your backing bean and populate your panel in backing bean.
# 6
thank you Y_NOT
the solution with putting a <h:panelGrid binding="#{myBean.grid}">
worked !!!
Youo dont even need to use the application context to create the Components...
HtmlCommandButton output = new HtmlCommandButton();
output.setValue("quack");
output.setId("quack1");
works perfectly too... thanks thanks thanks
Thanks a lot !!!
How come that there must be a panelGrid ? Do you know that ?
thx anyway
Message was edited by:
MichaelJackson12