Get Value from programmatically created components , Iterate over parent component to get child values in ADF Faces

Sharing is Caring

Sometimes we need to create and add ADF Faces components at runtime, we can do it programmatically
I have posted about this before
Creating a dynamic layout (form and UI Component) using ADF Faces

Now this post talks about getting value from the programmatically created component, means when the user enters a value in that component then how to access that value in the managed bean

Here I am using Jdev 12.1.3, Let’s see the implementation part

Created a page and added two buttons, one to create the component at runtime and the second one to get values from those components
See page XML source code –

<af:document title="ProgComponent.jspx" id="d1">
            <af:form id="f1">
                <af:spacer width="10" height="10" id="s3"/>
                <af:button text="Create Components " id="b1"
                           actionListener="#{viewScope.GetValueProgCompBean.createComponentsAction}"/>
                <af:spacer width="10" height="10" id="s1"/>
                <af:button text="Get Value" id="b2"
                           actionListener="#{viewScope.GetValueProgCompBean.getValueOfCompAction}"/>
                <af:panelGroupLayout id="pgl1" layout="vertical"
                                     binding="#{viewScope.GetValueProgCompBean.parentGroupBind}">
                    <af:spacer width="10" height="10" id="s2"/>
                </af:panelGroupLayout>
            </af:form>
        </af:document>

To create components at runtime I have used the same approach described in the above blog post
Check the code written on Create Components button

import javax.faces.component.UIComponent; 
import javax.faces.event.ActionEvent;

import oracle.adf.view.rich.component.rich.input.RichInputText;
import oracle.adf.view.rich.component.rich.layout.RichPanelGroupLayout;
import oracle.adf.view.rich.context.AdfFacesContext;

//Binding of panel group layout , it will be parent of prog. created components


private RichPanelGroupLayout parentGroupBind;

public void setParentGroupBind(RichPanelGroupLayout parentGroupBind) {
this.parentGroupBind = parentGroupBind;
}

public RichPanelGroupLayout getParentGroupBind() {
return parentGroupBind;
}

/**Method to add child components to parent
 * @param parentUIComponent
 * @param childUIComponent
 */
public void addComponent(UIComponent parentUIComponent, UIComponent childUIComponent) {
parentUIComponent.getChildren().add(childUIComponent);
AdfFacesContext.getCurrentInstance().addPartialTarget(parentUIComponent);
}

/**Method to create Input text at run time
 * @param actionEvent
 */
public void createComponentsAction(ActionEvent actionEvent) {
//Create an Object of desired UI Component
RichInputText ui = new RichInputText();
//Set properties
ui.setId("rit1");
ui.setLabel("Input text 1");
ui.setContentStyle("font-weight:bold;color:red");
//Now add this component to any parent component
addComponent(getParentGroupBind(), ui);

RichInputText ui1 = new RichInputText();
ui1.setId("rit2");
ui1.setLabel("Input text 2");
ui1.setContentStyle("font-weight:bold;color:red");
addComponent(getParentGroupBind(), ui1);
}

Now run the application and click on the button, you will see two inputText are created

Now to get the value of these components, follow the steps
1. Get Parent layout
2. Iterate over parent to get all child components
3. Get Value of every child

See the code written on get value button in the managed bean

    /**Method to Iterate over parent and get value of all child components
     * @param actionEvent
     */
    public void getValueOfCompAction(ActionEvent actionEvent) {
        RichPanelGroupLayout PF = getParentGroupBind(); // Get Parent Layout
        List<UIComponent> listcomp = PF.getChildren(); // Get all child
        Iterator iter = listcomp.iterator(); // Create an Iteraotr to iterate over childs
        while (iter.hasNext()) {
            UIComponent comp = (UIComponent) iter.next(); // Get next Child Component
            System.out.println("Component is-" + comp + " and value is-" +
                               comp.getAttributes().get("value")); //Get Component detial and it's value
        }
    }

Again check, enter the value in both inputText and click on the button to get value

Get Value

The output on console-

Sample ADF Application- Download
Cheers 🙂 Happy Learning

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *