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);



BindingContext bctx = BindingContext.getCurrent();
BindingContainer bindings = bctx.getCurrentBindingsEntry();
JUCtrlListBinding list =    (JUCtrlListBinding) bindings.get(“DepartmentId”);
Row selectedRow = (Row)list.getSelectedValue();
String selectedValue = list.getAttributeValue().toString();
System.out.println(“old department is ” + selectedValue );
list.setSelectedIndex(Integer.parseInt(valueChangeEvent.getNewValue().toString()));
selectedValue = list.getAttributeValue().toString();
System.out.println(“new department is ” + selectedValue);
Advertisements

16 Comments

Filed under ADF

16 responses to “Reading the selected value of a select list in Java

  1. Chris Monte

    Awesome, I ran into this when ADF/JSF was released in 2000 (Version .98) I could not remember how to get the actual values from the selectOneChoice component. Thanks for the help.

  2. sanjeeb

    Hey this is fine getting the selected value in java backing bean or any other class but suppose i need to get the value of that drop down in jsff page how do i do that.??
    if i access the binding value of DepartmentId i get the index value. I need to get the actual value selected so that i can manipulate enabling/disabling of other components based on the value selected for DepartmentId.

    • Hello Sanjeeb,
      does your LOV and input test within the same jsff fragment, if yes you can use the second method, it does not matter if you have a JSF page or a jsff fragment.

  3. sergio

    Hi,
    could you please post the related imports that the bean needs ?
    Thanks,
    Sergio.

    • import javax.faces.context.FacesContext;
      import javax.faces.event.ValueChangeEvent;
      import oracle.adf.model.BindingContext;
      import oracle.binding.BindingContainer;
      import oracle.jbo.Row;
      import oracle.jbo.uicli.binding.JUCtrlListBinding;

  4. Greg

    Hi majbr,
    I did exactly as mentioned in your
    “The second method is by creating a secondary binding for DepartmentId”

    It returns me the index value i.e. 1 instead of the actual value which is 25.

    Can you please help

    • Hello,
      I don’t know why you get the old value.
      did you add these two lines of code before reading the value?
      FacesContext contxt = FacesContext.getCurrentInstance();
      valueChangeEvent.getComponent().processUpdates(contxt);

      Did you try the first method?
      both method should lead to the same result.

  5. Fonz

    Hello,

    This is a wonderful writeup. How could I use this approach to retrieve multiple selected values from a SelectManyListbox.

  6. Fonz

    Hi Mohammad. This was exactly what I wanted. Thank you!! The only difference was that my values were strings, which seem to work as well.

    My only issue is that I need to find out what the user has selected in my validator method. I’ve added the code you sent me in my validator method, and the list I am getting back seems to be one step behind what the user is selected. For example, when the page first load and the user checks “foo1”, the validator method will retrieve an empty selected list. When the user checks foo2, while keeping foo1 checked, the validator method will find one selectedItem, “foo1”.

    My question is how, can write my validator method so that its in sync with what the user is selecting.

    • I don’t understand you, where do you want to read the selected values?
      If in it is inside valueChangeLsitener method, try to add these two lines of code at the beginning of the method:
      FacesContext contxt = FacesContext.getCurrentInstance();
      valueChangeEvent.getComponent().processUpdates(contxt);

      If the this not the case, you can share your use case in OTN where you can find many experts to help you.
      https://forums.oracle.com/forums/forum.jspa?forumID=83

  7. Saif

    Mohammad,
    Those 2 lines of code is what I needed.. Thank you sooo very much..
    It works absolutely fine

  8. Alok

    Thanks for the code…I was struggling to get it working..your blog helped me to resolve the issue…

  9. suganya

    hi….i need the same requriements…i read ur blog…but i didnt get one line what ur saying….plz clear this

    After that change the id value to DepartmentIdValue.

    • Mohammad Jabr

      Hello suganya,
      I meant to change the Id value for newly created attribute, to do this go to your PageDefinition file and select the new attribute and then change its Id value from the property inspector.

  10. Pingback: ADF : récupérer la valeur choisie d'une liste de sélection | ArKZoYd

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s