dynamicaly adding components to panel/grid

I am trying to add components dynamically in panelgrid but it is displaying nothing.in jsp tag is

<h:panelGrid id = "ExpenseDetail" binding = "#{expenses.component}" />

tand his is what i am doing in backing bean.

public HtmlPanelGrid getComponent()

{

FacesContext facesContext = FacesContext.getCurrentInstance();

Application application = facesContext.getApplication();

component = new HtmlPanelGrid();

component.getChildren().clear();

component.setBorder(1);

component.setColumns(1);

component.setStyleClass("browsetabletype2");

component.setWidth("90%");

UIOutput outText = (UIOutput)application.createComponent("javax.faces.HtmlOutputText");

outText.setValue("Name");

outText.setId("Name");

component.getChildren().add(outText);

return component;

}

[888 byte] By [shila] at [2007-10-2 18:25:02]
# 1

What about setter of your binded component ?

Try this

private HtmlPanelGrid dynamicPanel = null;

public HtmlPanelGrid getDynamicPanel {

if (this.dynamicPanel == null) {

// this will happen the first time the HtmlPanelGrid is retrieved

this.dynamicPanel = new HtmlPanelGrid();

}

try {

generatePanel(this.dynamicPanel);

} catch (Exception e) {

e.printStackTrace();

}

return dynamicPanel;

}

public void setDynamicPanel(HtmlPanelGrid dynamicPanel) {

try {

generatePanel(dynamicPanel);

} catch (Exception e) {

e.printStackTrace();

}

this.dynamicPanel = dynamicPanel;

}

private void generatePanel(HtmlPanelGrid dynamicPanel){

dynamicPanel.getChildren().clear();

dynamicPanel.setId("dynamicPanel_1");

//adding other attributes and children ...

}

I always use this method, and it works. Maybe there is more sophisticated way, but I didn't managed to find it. Hope it helps.

Martin

mandrzeja at 2007-7-13 19:46:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
hello martin ,thanks for reply .my code is working now but do you know how to add or delete row of components.shilpa
shila at 2007-7-13 19:46:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

How do you know how many rows, and what type of components should your panelGride have ?

I'm generating my forms acording to values from DB.

So I have ArrayList of objects with values from DB. Then i iterate over this list and create whole panelGrid with components. If i want to delete row, i remove from list some objectes. If I want to add row, I add specified number of objects into list.

But I think it's not what you expecting.

Martin

mandrzeja at 2007-7-13 19:46:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
my row is of 1 checkbox and 1 textarea and 6 inputtext componets.also i want to put headings for each.shilpa
shila at 2007-7-13 19:46:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

If U want to add header to generated panelGrid try this

HtmlPanelGrid panelGrid = new HtmlPanelGrid();

Map facets = panelGrid.getFacets();

HtmlOuputText outputText = new HtmlOutputText();

outputText.setValue("MY header text");

facets.put("header",outputText );

If Your list of elements on panelGrid does'nt change dynamicly, why are U using component binding and dynamicly generated components ?

Martin

mandrzeja at 2007-7-13 19:46:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
If i have entered 5 rows of components and now i want to delete two of them using checkboxes clicked .how could i remove those rows from w/o using panelgrid.shilpa
shila at 2007-7-13 19:46:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

never done this before, but try to iterate over panelGrid's childrens

panelGrid.getChildren();

check if each of children is HtmlSelectBooleanCheckbox

with instanceof

If its checkbox and value of its true then remove

all components from i to column count (where i is your courrent iteration position)

panelgrid.getChildren().remove(i);

But I realy don't know if it's gonna work :)

Good luck ;)

Martin

mandrzeja at 2007-7-13 19:46:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
ok .thanks.
shila at 2007-7-13 19:46:02 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...