Follow by Email

Thursday, 21 January 2016

Add and delete values in POJO based selectOneListbox/selectOneChoice in ADF

Previously i have posted about populating selectOneChoice programmatically using POJO
Programmatically populate values in a af:selectOneChoice component in ADF

In same way we can populate values in selectOneListBox as both ADF Faces components are used for single selection and share same structure

Here i have used a List to populate values in selectOneListBox (For details read complete article in above link)
And to get selected value from af:selectOneListBox/selectOneChoice in bean- Used a String variable , created it's accessors

    //List DataStrucutre to populate values in selectOneListBox
    List<SelectItem> customList = new ArrayList<SelectItem>();

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

    public List<SelectItem> getCustomList() {
        return customList;

    //String variable to hold selectOneListBox value
    private String selectedVal;

    public void setSelectedVal(String selectedVal) {
        this.selectedVal = selectedVal;

    public String getSelectedVal() {
        return selectedVal;

And see here how both List and String variable are bound to af:selectOneListBox

<af:selectOneListbox id="sol1" contentStyle="width:150px;" size="10" value="#{viewScope.SelectOneListBoxBean.selectedVal}">
                                    <f:selectItems value="#{viewScope.SelectOneListBoxBean.customList}" id="si1" var="variable"/>

Now I have dropped an inputText and two buttons on page to add and delete values from selectOneListBox. Created component binding for inputText to get Value in managed bean

<af:panelGroupLayout id="pgl1" layout="vertical">
                            <af:inputText label="Enter value to add in Listbox" id="it1"
                            <af:button text="Add Record" id="b1"
                            <af:spacer width="0" height="10" id="s1"/>
                            <af:panelGroupLayout id="pgl2" layout="horizontal">
                                <af:selectOneListbox id="sol1" contentStyle="width:150px;" size="10"
                                                     partialTriggers="b1 b2">
                                    <f:selectItems value="#{viewScope.SelectOneListBoxBean.customList}" id="si1"/>
                                <af:button text="Delete" id="b2"

Now see code for add and delete buttons, Add button actionListener get value from inputText using component binding and add it to List  and Delete button finds and deletes selected value from List

    /**Method to add Record in List
     * @param actionEvent
    public void addValueInList(ActionEvent actionEvent) {
        //Get value from inputText using component binding
        if (itBind.getValue() != null) {
            //Add value in List
            customList.add(new SelectItem(itBind.getValue().toString()));

    /**Method that check for selected value in List
     * @param items
     * @param value
     * @return
    public SelectItem getItem(List<SelectItem> items, String value) {
        for (SelectItem si : items) {
            if (si.getValue().toString().equalsIgnoreCase(value)) {
                return si;
        return null;


    /**Method to delete selected record from List
     * @param actionEvent
    public void deleteSelectedValue(ActionEvent actionEvent) {
        if (selectedVal != null) {
            //Find and delete selected item from List
            customList.remove(getItem(customList, selectedVal));

All Done :) Run and check application
Enter a value in inputText and click on Add button , It is added in ListBox

 Add 3 values and select second value to delete

 Click on delete button

Sample ADF Application - Download
Cheers :) Happy Learning

No comments :

Post a Comment