« JDeveloper Twitter Feed Has Moved | Main | JDeveloper 11.1.1.2 is out »

Getting the Value from a SelectOneChoice List Using Code

I got asked this today, and this seems to be a question that pops up every now and again, so I thought I'll document this little piece of code.

The scenario is that you have a drop down list using a selectOneChoice component on your page - and you want to find out the selected value in a backing bean.
For example the departmentId field in this page:

The first thing everyone try and do is to access the list component in a backing bean and invoke the getValue method on it. But then they realize that this only returns the actual index of the row that was selected and not the value.

The following little piece of code will get you that value.
What it does is access the bindings object, get the list binding, and then invoke the getSelectedValue method on it.

public void buttonPressed(ActionEvent actionEvent) {

// Get the binding
BindingContainer bindings =
BindingContext.getCurrent().getCurrentBindingsEntry();
// Get the sepecific list binding
JUCtrlListBinding listBinding =
(JUCtrlListBinding)bindings.get("DepartmentId");
// Get the value which is currently selected
Object selectedValue = listBinding.getSelectedValue();
System.out.println(selectedValue);
}


Note that the binding name I'm getting (DepartmentId) is the last one in this picture - see the list binding icon next to it.
lov002.gif

Comments (1)

The other trick that I use all the time is to create AttributeBindings based on the same iterator used for the listBinding. This way instead of coding I can just use EL to grab the current value. The only difference in this case is that you need to set the selectOneChoice to autoSubmit so the attributeBinding gets updated with the selected value. This is a good trick if you need to show a description write to the side of your selectOneChoice, or anywhere else in the page.

[]s! :-)

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)