File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes Creating Datatable dynamically Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Creating Datatable dynamically" Watch "Creating Datatable dynamically" New topic

Creating Datatable dynamically

ved gunjan
Ranch Hand

Joined: May 17, 2003
Posts: 86
My requirment is that i have to create a dynamic datatable and which contains listing of Questions and its respectives Answer. These questions and answer come from the server and along with the answer i also get the answer type , which tells what is the type of the UIComponent that is associated with the answer say input text,select many,select one etc., so i have to dynamically create these UIComponent inside the datable based on the answer type.
Actually i'm having the logic to create the components which is based on the answer type which comes from the backend and this answer type define what kind type of component(component for single answers/component for multiple answer,input and other types) needs to be created.(this will be done in the if-else block)

My problem is that im not able to create this componets dynamically in the data table.
In the code below , i have binded the datatable and in the java method i 'm trying to create the ui components(as of now im creating it without the creation logic of answer type).
The idea was to just try and see whether the components are created and is getting added to the datable.

when i run this code im getting no output.

Please suggest me where im going wrong and what is the correct approach.

I'm really in a dire straits.

Here's the code:



<h:dataTable id="mainTable" binding="#{dynamicQuestions.dataTable}" value="#{requirementBean.questionList}"

var="questions" border="1"/>

Java method


public UIData getDataTable()


Application application = FacesContext.getCurrentInstance().getApplication();
UIViewRoot uiViewRoot = FacesContext.getCurrentInstance().getViewRoot();
HtmlDataTable dataTable =(HtmlDataTable)uiViewRoot.findComponent("mainTable");

//------------------- adding HtmlOutputText ---------------//
String vbStr = "#{questions.questionData}";
ValueBinding vb = FacesContext.getCurrentInstance().getApplication().createValueBinding(vbStr);
HtmlOutputText output = new HtmlOutputText();
output.setValueBinding("value", vb);

//------------------- adding HtmlSelectOneMenu ---------------//
vbStr = "#{questions.itemList}";
vb = FacesContext.getCurrentInstance().getApplication().createValueBinding(vbStr);

HtmlSelectOneMenu hsom = new HtmlSelectOneMenu();

//------------------- adding HtmlSelectManyListbox ---------------//

vbStr = "#{questions.itemList}";
vb = FacesContext.getCurrentInstance().getApplication().createValueBinding(vbStr);

HtmlSelectManyListbox hsmlb = new HtmlSelectManyListbox();

//datatable column
UIColumn column = new UIColumn();


return dataTable;



Ravisekhar Kopparthi

Joined: Jul 12, 2006
Posts: 5
JSF has a complex lifecycle. It all boils down to if you have considered the JSF life cycle in the code that you pasted.

To me it appears that the <h:datable> is rendered before calling your method.

I think you need to use IBM's scriptcollector
<h:ScriptCollector pre-Render ="onPageLoadBegin()">

and put all the pre render logic in onPageLoadBegin(facesContext)

Hope this helps.

Ravisekhar Kopparthi
I agree. Here's the link:
subject: Creating Datatable dynamically
It's not a secret anymore!