Create ADF Choice List and apply selectItems to it programmatically

Sharing is Caring

I hope we all know how to create ADF Choice List (selectOneChoice) using Model layer and previously I have posted about populating selectOneChoice programmatically from the bean using ArrayList

Programmatically populate values in an af:selectOneChoice component in ADF

But this post is slightly different – It is about creating a 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 a 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 the page and created an ActionListener in the managed bean, In this method, I am creating a 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 the button, SelectOneChoice is created πŸ™‚

Create ADF Choice List

Sample ADF Application (Jdev 12.1.3) – Download
Cheers πŸ™‚ Happy Learning

Related Posts

Leave a Reply

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