Dynamically set selected values checkBoxGroup object.

Hi all,

I am running into a problem setting selected boxes from a checkBoxGoup. The problem is that I want to dynamically set the selected state of the boxes as I select a value from a listbox. (All on the same page).

The checkBoxGroup items are a representation of a table in the database. When the page is opened and the listbox is changed this fires a valuechanged event. In this event I would like to check some boxes according to the selection made.

Does anyone has an idea what I can do to set these checks ?

I have already seen that there is a getSelected() method which I use to get the selection in an objectarray.

if (checkboxGroup1.getSelected() != null) {

Object[] mailType = (Object[])checkboxGroup1.getSelected();

mailTypeSize = mailType.length;

}

I alreay tried to do the opossite, by using the setSelected() method. But till now without any satisfying result.

Thanx in advance...........

[977 byte] By [seve01a] at [2007-10-3 9:42:14]
# 1

Where is the get/setSelected() method(s)? Within a HTMLSelectManyCheckbox I see a get/setSelectedValues() method(s).

Anyways, let's just assume it's a typo or I am reading the wrong API docs. To set the selection, all you have to do is set the value attribute of the selectManyCheckbox from within the listboxes valueChangeEvent. It's actually easier done than explained! =)

I'm assuming that you have a tag on your JSP that looks like this (for the selectManyCheckbox component):

<h:selectManyCheckbox id="whatever"

value="#{BackBean.selectedListItems}" layout="pageDirection">

<f:selectItems value="#{BackBean.listOptions}" />

</h:selectManyCheckbox>

In your backing bean, you should have a get/setSelectedListItems methods and at least a getListOptions method. The getListOptions method will return a list or array of SelectItem objects. The SelectItem object will take some object as the value. The setSelectedListItems method will receive the selection from the list. So it takes an array of whatever objects used as the value of the SelectItem objects as an argument.

Therefore, if you want to set the selection of the selectManyCheckbox you can just call the setSelectedListItems() method. Set the selection to an appropriate array. Do this from within your valueChangeListener fired from the listbox.

This next part is important, so make sure you do it! Make sure that the listbox has immediate="true" set. AND at the end of your valueChangeListener, you must call facesContext.renderResponse(). This will skip the remaining phases on any other components. If you don't, the values you set from the valueChangeListener will get overwritten by the remaining phases.

Hope this helps,

CowKing

IamCowKinga at 2007-7-15 4:58:24 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...