Thursday, June 18, 2009

How to implement drop down list in ADF

In this how-to example I am defining a department drop down list, which shows a list of departments.

Add the following code in the ADF page.

          <af:selectOneChoice label="Department"
value="#{ModelData.department}">
<f:selectItems value="#{DepartmentOptions.departments}"/>
</af:selectOneChoice>

In ADF, selectOneChoice is used for drop down list.

selectOneChoice picks the list of values from 'selectItems' tag.

You can define a managed bean to bind an ArrayList of SelectItem objects to 'selectItems'. or can define a view object and bindings. In this example I have defined a managed bean which provides an ArrayList of SelectItem objects.

Department list Backing bean code.

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

import javax.faces.model.SelectItem;

public class DepartmentOptions {
private List departments;

public DepartmentOptions() {
departments = new ArrayList();
SelectItem department = new SelectItem("10", "Electric");
departments.add(department);
department = new SelectItem("20", "Mechanic");
departments.add(department);
department = new SelectItem("30", "Computer");
departments.add(department);
}

public void setDepartments(List departments) {
this.departments = departments;
}

public List getDepartments() {
return departments;
}
}

0 comments: