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:

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! :-)

Posted by Maiko Rocha on November 06, 2009 at 03:31 AM PST #

Thanks Shay, this was indeed what i was looking for too.

Posted by Jayaram on June 18, 2010 at 01:05 AM PDT #

Hi Shay,

I used your code in the AM method and it complied fine.
But when I run it, it gives the below error

oracle.adfinternal.view.faces.model.binding.FacesCtrlAttrsBinding cannot be cast to oracle.jbo.uicli.binding.JUCtrlListBinding

Why I am putting it in AM method? Because based on this value I am executing another view.

Thanks for your help.

Posted by guest on October 06, 2011 at 05:01 AM PDT #

Guest - you are doing something wrong if you are using faces/view layer classes in your AM. The AM is suppose to be independent from any specific View/client technology.
The right approach is to use AM service method, see:
http://blogs.oracle.com/shay/entry/am_service_method_-_simple_dem

Then you can even bind the parameter to an LOV, see:
http://blogs.oracle.com/shay/2011/01/adding_an_lov_to_a_query_param.html

Posted by Shay on October 06, 2011 at 07:05 AM PDT #

Hi Shay,

I was googling around when I bump into your post.

In this particular post of yours, you are getting the value from the select one choice.

I have this question, what if you want to set the value of the selectonechoice which is bound in the bindings? Is there a utility method that will set the value of the JUCtrlListBinding

A sample use case is that, for example in other parts of the UI, you selected something and you need to update the DepartmentId to a particular value from the list then how can you do this?

Thanks

Marky

Posted by guest on September 27, 2012 at 02:10 AM PDT #

HI Shay,

Very well put together.

Posted by Achinto on December 21, 2012 at 06:05 AM PST #

I am using backing bean to populate the list instead of data binding and when I am trying to get the value, getting index. Pls let me know how to get the actual value in this case.

Posted by guest on December 28, 2012 at 08:14 AM PST #

Hi Shay,
I am able to resolve the issue as by reading the label information of that particular select item rather than value. Reading value of the select item gives numeric number where as reading label gives the description.
Thanks a lot for providing the valuable information's.

Posted by guest on January 07, 2013 at 07:45 PM PST #

This won't work if you define the LOV in the VO. It doesn't show up in the bindings like you show.

Also, I'm not sure it will work if your selectonechoice is in an AF:Table.

Posted by guest on August 27, 2013 at 11:02 AM PDT #

Ok, I think I finally got this worked out.

If you're in a validator, you don't have access to the "value change" stuff.

So, I did this:

MyConfViewRowImpl vConfStat = (MyConfViewRowImpl)JSFUtils.resolveExpression("#{row.bindings.CnfrRqstStatCd.valueList[" + pObject + "]}");
String vNewCdString = vConfStat.getCnfrRqstStatCd();

Posted by guest on August 27, 2013 at 11:55 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

me
I'm a Director of Product Management for the Oracle Java Development Tools.
Follow me:
Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today