Wednesday, March 2, 2011

How to create ADF components on runtime?

Quite easy actually! There are two methods by which we can do it. Either we can create the component in the managed bean and add it to the children of an existing component, or we can create it in our .jspx or .jsff page directly.

1) Creating component in the Managed Bean.

In the below code we are creating a new ShowDetailItem which we will add to an existing panelTabbed item.

private UIComponent createComponent() {
UIComponent componentToAdd = null;

//Create new object of ShowDetailItem and set its properties.
RichShowDetailItem item = new RichShowDetailItem();
item.setDisclosed(true);
item.setText("new tab");
componentToAdd = item;

//Now that we are at it, I am creating a new iFrame which will be set inside the ShowDetailItem
RichInlineFrame frame = new RichInlineFrame();
frame.setSource("http://oracle.com");

//add the iFrame to the children of the ShowDetailItem
componentToAdd.getChildren().add(frame);

return componentToAdd;
}

Now calling this method from the event listener on which I want to add the ShowDetailItem:
....
....
RichPanelTabbed mainPanel = getMainPanelTabbed();
UIComponent componentToAdd = buildComponent();
mainPanel.getChildren().add(componentToAdd);
....

And we are done! Now when ever this event listener is invoked, you will see a new tab in your panelTabbed layout.

2) Second way is also very easy and preferred if you have to create more than one components of same type. So if I want to create multiple tabs at run time for my panelTabbed component, I should use this method.

This is by using <af:iterator>> in your page. See the snippet below:

<:iterator var="row"
value="#{bindings.selectedFormsIterator.allRowsInRange}">
<af:showDetailItem text="#{bindings.selectedFormsIterator.currentRow.dataProvider.formName}">
<af:inlineFrame source="http://someurl.com/test.jsp?Form=#{bindings.selectedFormsIterator.currentRow.dataProvider.formName}" />
</af:showDetailItem>
</af:iterator>

Basically af:iterator is a modified version of af:forEach and is suggested for iteration if you are trying to create multiple components.

You just need to create the iterator binding and associate it with your iterator and you are ready to go!

1 comment:

  1. Hi,
    I'm new in ADF.
    I created adf components on runtime , now i want to make these components selectable to delete a specific one on runtime.
    any help plz,
    ps: Sorry for my bad english

    ReplyDelete