Follow by Email

Saturday, 30 April 2016

Create ADF Choice List and apply selectItems to it programmatically

I hope we all know how to create selectOneChoice using Model layer and previously I have posted about populating selectOneChoice programmatically from bean using ArrayList

Programmatically populate values in a af:selectOneChoice component in ADF

But this post is slight different - It is about creating selectOneChoice component and applying selectItems to it programmatically
There is nothing complicated in this , Just a piece of code is required
Here I have used an ArrayList to set SelectItems and created component binding for af:form to add selectOneChoice as it's child 

Packages used in bean-


import java.util.ArrayList;
import java.util.List;

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

import javax.faces.model.SelectItem;

import oracle.adf.view.rich.component.rich.RichForm;
import oracle.adf.view.rich.component.rich.input.RichSelectOneChoice;



    //af:form binding to add selectOneChoice as it's child
    private RichForm rootFormBind;

    public void setRootFormBind(RichForm rootFormBind) {
        this.rootFormBind = rootFormBind;
    }

    public RichForm getRootFormBind() {
        return rootFormBind;
    }
   
    //List variable to populate selectOneCoice
    List<SelectItem> customList;

    public void setCustomList(List<SelectItem> customList) {
        this.customList = customList;
    }

    public List<SelectItem> getCustomList() {
        if (customList == null) {
            customList = new ArrayList<SelectItem>();
            customList.add(new SelectItem("i1", "Item 1"));
            customList.add(new SelectItem("i2", "Item 2"));
            customList.add(new SelectItem("i3", "Item 3"));
            customList.add(new SelectItem("i4", "Item 4"));
            customList.add(new SelectItem("i5", "Item 5"));
        }
        return customList;
    }

Dropped a button  on page and created an ActionListener in managed bean , In this method I am creating selectOneChoice component and adding selectItems to it


    /**Method to create SelectOneChoice programmatically and add Items to it
     * @param actionEvent
     */
    public void createSOCAction(ActionEvent actionEvent) {
        //Create SelectOneChoice component using It's Java Class
        RichSelectOneChoice selectOneChoice = new RichSelectOneChoice();
        //Set required proprties of component
        selectOneChoice.setId("soc1");
        selectOneChoice.setLabel("Choice List");

        //Create SelectItems component
        UISelectItems selectItems = new UISelectItems();
        //Set Id
        selectItems.setId("si1");
        //Set SelectItems value, It'll be derived from an ArrayList of type javax.faces.model.SelectItem
        selectItems.setValue(getCustomList());

        //Add selectItems component as child of selectOneChoice
        selectOneChoice.getChildren().add(selectItems);

        //Finally add SelectOneChoice to any root component of page
        getRootFormBind().getChildren().add(selectOneChoice);
        AdfFacesContext.getCurrentInstance().addPartialTarget(rootFormBind);
    }

Now run and check application- On click of button , SelectOneChoice is created :)

Sample ADF Application (Jdev 12.1.3) - Download
Cheers :) Happy Learning

No comments :

Post a Comment