This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes JSF and the fly likes Selecting some entries in selectManyListbox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Selecting some entries in selectManyListbox" Watch "Selecting some entries in selectManyListbox" New topic
Author

Selecting some entries in selectManyListbox

Ben Gateau Jr
Greenhorn

Joined: Jan 08, 2009
Posts: 17
Hi all,

I'd like someone to advise me how to have (only) some entries in selectManyListbox selected. I have many-to-many relationship between Clients and Products, and when I was adding new Client I was populating selectManyListbox with all the available Products. Everything was correctly inserted into database, and now I am trying to create edit page. This selectManyListbox should contain all the available products (like before), but I would like to have Client's products selected. So in the backing bean I have list with all the products in form of List<SelectItem> (used earlier when adding), and I have List<Product> retrieved from the database. SelectItem entries where created using product id and name (ie, new SelectItem(product.getId(),product.getName())). And I don't know how to combine them to have what I described earlier. <f:selectedItem> and <f:selectedItems> don't even have 'selected' attribute, or something similar. BTW, I am using MyFaces 1.2 and Facelets.

Regards, Ben.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
You need to preset them in the 'value' attribute of the input component.
Ben Gateau Jr
Greenhorn

Joined: Jan 08, 2009
Posts: 17
Hi Bauke,

Thank you for your reply. Can you please explain what you mean by 'value of the input component'? When translated to HTML, SELECT component contains OPTIONs whose values are product ids, and product names are displayed for viewing. What I need is to have SELECTED for those options whose id matches products ids in List<Products>. I can match them easily I think, but have problems marking them as SELECTED using selectManyListbox.

Regards, Ben.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
All input components have a 'value' attribute which you normally bind to a bean property. If you want to give it another default value than null, then just preset it in beanĀ“s constructor or so.
Ben Gateau Jr
Greenhorn

Joined: Jan 08, 2009
Posts: 17
Bauke, perhaps I wasn't clear in my first post ... I have binding to the bean property in place and the 'value' attributes are all set. What I am after is a way of programatically selecting some of the items displayed in the listbox. I know the values of the items I want to select, I just need to know how to select them.

Regards, Ben.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Yes .. As I said, preset the property behind the value attribute. You can do that in the bean constructor.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Do you not have something like, setSelectedValues(Object[] selectedValues) for your component?
anna yuan
Greenhorn

Joined: Feb 04, 2009
Posts: 1

1. backingBean.allProducts -->list for all products
2. backingBean.selectProductList --> list for client's products retrieved from the database.

<s:selectManyPicklist id="selectProductList" style="width:300px; height: 340px;" value="#{backingBean.selectProductList}" size="20" styleClass="inputText">
<f:selectItems value="#{backingBean.allProducts}" />
</s:selectManyPicklist>
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Here is an example:



This will show a multiselect listbox with 5 items labeled "label1" up to "label5" where the "label2" and "label4" are pre-selected.

Got it now? It is nothing different from how all other input components work. Just preset the default value in the property behind the 'value' attribute.
Ben Gateau Jr
Greenhorn

Joined: Jan 08, 2009
Posts: 17
Thank you anna and Bauke for the examples. I've made a mistake of not posting my code, it would be clear what was wrong. I had 'value' attribute set but only on 'selectItems'. That items can be selected by setting the 'valule' of selectManyListbox - I would not have guessed it for a long time. Thank you both so much for your help and assistance, and Bauke I appreciate your perseverance

Regards, Ben.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Selecting some entries in selectManyListbox