Tuesday, 17 February 2015

Reading the selected value of a select list in Java

Reading the selected value of a select list in Java

Environment (JDeveloper 11.1.1.x, ADF BC, HR schema)
A frequent question on OTN is how to read the selected value not its index of    a select list in Java (This case exists in JDeveloper 11.1.1.x. because an expression like #{bindings.JobId.inputValue} would return the internal list index number when JobId was a list binding. In JDeveloper 11.1.2 this is no longer needed, the #{bindings.JobId.inputValue} expression will return the attribute value corresponding with the selected index in the choice list.) . in this post I will illustrate two methods to do this. I will assume that you already built your select list either model driven or dynamic select list.  Both of them are bound to the JUCtrlListBinding in the associated binding container.
Based on employee table in HR schema, where  the employee’s  DepartmentID attribute is bound as a list and populated from the DEPARTMENTS table,we need to read the department ID from the list in the valueChangeListener method using one of the follwing methods:
  • public void departmentIdChanged(ValueChangeEvent valueChangeEvent)
    {
    // Add event code here…
    BindingContext bctx = BindingContext.getCurrent();
    BindingContainer bindings = bctx.getCurrentBindingsEntry();
    JUCtrlListBinding list=(JUCtrlListBinding)bindings.get(“DepartmentId”);
    Row selectedRow = (Row)list.getSelectedValue();
    String selectedValue = list.getAttributeValue().toString();
    String deptName= selectedRow.getAttribute(“DepartmentName”).toString();
    System.out.println(“old department ID is ” + selectedValue );
    System.out.println(“old department name is ” + deptName);
    FacesContext contxt = FacesContext.getCurrentInstance();

    valueChangeEvent.getComponent().processUpdates(contxt);
    selectedRow = (Row)list.getSelectedValue();
    selectedValue = list.getAttributeValue().toString();
    deptName=  selectedRow.getAttribute(“DepartmentName”).toString();
    System.out.println(“new department ID is ” + selectedValue);
    System.out.println(“new department name is ” + deptName);

    }
  • The second method is by creating a secondary binding for DepartmentId and get its value as the follwings:
  1. From your page, right click and select Go to Page Definition.
  2. From the binding section, click the plus green icon to create a new control biding, and choose attributeValues from the list as shown in the  image below:
    create secondary attributeValues biding
  3. select your data source from the list, then select the DepartmentId attribute, as shown in the image below.After that change the id value to DepartmentIdValue.
    Data source and attribute selection
    DataSource and attribute selection
  4. Now you can access the DepartmentId value by using the following code:
public void departmentIdChanged(ValueChangeEvent valueChangeEvent)
{
// Add event code here…
BindingContext bctx = BindingContext.getCurrent();
BindingContainer bindings = bctx.getCurrentBindingsEntry();
 AttributeBinding deptIdBinding =   (AttributeBinding)bindings.getControlBinding(“DepartmentIdValue”);
String deptId = deptIdBinding.getInputValue().toString();
System.out.println(“old department ID ” + deptId );
FacesContext contxt = FacesContext.getCurrentInstance();

valueChangeEvent.getComponent().processUpdates(contxt);
deptId = deptIdBinding.getInputValue().toString();
System.out.println(“new department ID ” + deptId);

}
Note:
you can replace the bold cod, in the previous two methods
FacesContext contxt = FacesContext.getCurrentInstance();
valueChangeEvent.getComponent().processUpdates(contxt);
with this one, and both code segments lead to same result
int index;
index=Integer.parseInt(valueChangeEvent.getNewValue().toString());
list.setSelectedIndex(index);

No comments:

Post a Comment