setting check mark for SelectManyCheckbox item

I'm trying to set the check mark for a SelectManyCheckbox item from a backing bean and cannot find any explicit explanation or example to do this. This would be used to have a command button select all checkboxes and a command button to clear all checkboxes.

I've been trying to set the SelectItem value, but while log messages suggest what is desired has occurred, nothing visually changes. Here's the key parts of what I'm working with so far.

<h:selectManyCheckbox

id="patients" layout="pageDirection"

value="#{SelectBean.patientsSelected}" >

<f:selectItems value="#{SelectBean.patientSelects}" />

</h:selectManyCheckbox>

<h:commandButton type="submit" value="Select All Patients"

id="allPatients"

action="update"

actionListener="#{SelectBean.onSelectAll}" />

private Collection<SelectItem> patientSelects;

private List<String> patientsSelected;

for (int i = 0; i < 10; i++){

value =false;

label = locationSelected +"-" +"patient" + Integer.toString(i);

patientSelects.add(new SelectItem(value, label));

}

publicvoid onSelectAll(ActionEvent event){

for (Iterator it = patientSelects.iterator(); it.hasNext();){

SelectItem si = (SelectItem) it.next();

si.setValue(true);

}

[2116 byte] By [paul@sdna] at [2007-11-27 9:21:54]
# 1
You want to preselect and unselect items in the backing bean? Simply do it on patientsSelected.
BalusCa at 2007-7-12 22:16:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

I don't understand what you mean by do it on an ArrayList of Strings. Do what?

Perhaps I wasn't clear. I'm trying to preselect/unselect items from within the backing bean. Such changes then need to appear in the view.

While patientsSelected returns an array of the selected items, what would a new value do - nothing I tried did anything. Especially when trying to preselect all items - the arrayList you suggest doesn't even contain these items.

Are you aware of any examples or documentation for this concept of non-interactively setting the graphical select state of a checkbox? This seems so simple a requirement, but I've yet to find it referred to anywhere - let me list the books....

paul@sdna at 2007-7-12 22:16:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

> the arrayList you suggest doesn't even contain these items.

You have to prefill the patientsSelected yourself to mark preselected items.

Basic example:<h:selectManyCheckbox value="#{myBean.selectedItems}" />

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

</h:selectManyCheckbox>

MyBeanprivate List<String> selectedItems; // + getter + setter

private List<SelectItem> selectItems; // + getter + setter

{

// Prefill selectItems.

selectItems = new ArrayList<SelectItem>();

selectItems.add(new SelectItem("value1", "label1"));

selectItems.add(new SelectItem("value2", "label2"));

selectItems.add(new SelectItem("value3", "label3"));

selectItems.add(new SelectItem("value4", "label4"));

selectItems.add(new SelectItem("value5", "label5"));

// Preselect item #2 and #4.

selectedItems = new ArrayList<String>();

selectedItems.add("value2");

selectedItems.add("value4");

}

BalusCa at 2007-7-12 22:16:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Most of what you suggest is needed is in place and works fine. But the last about filling the 'selected' list turned the light on.

But it's difficult to believe the JSF design logic for this; you have maintain two separate constructs 'mapped' by a common value just to represent a certain state of one item!!! How about an attribute/property - see selectBooleanCheckbox!!!

These are the two methods as actionListeners on my commandButtons that made it work for me:

public void onSelectAll(ActionEvent event) {

for (Iterator it = patientSelects.iterator(); it.hasNext();) {

SelectItem si = (SelectItem) it.next();

patientsSelected.add((String)si.getValue());

}

}

public void onClearAll(ActionEvent event) {

patientsSelected.clear();

}

paul@sdna at 2007-7-12 22:16:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
I don't understand the new question or problem clearly?
BalusCa at 2007-7-12 22:16:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
There is no new question or problem. Your last suggestion made me think of how to address my issue. The code presented is what made it work for me and may help someone else. thanks again.Did I actually award you the Duke Stars? It's tough to know if my attempt worked.
paul@sdna at 2007-7-12 22:16:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
> thanks again.yw> Did I actually award you the Duke Stars? It's tough to know > if my attempt worked.I don't care about them.
BalusCa at 2007-7-12 22:16:01 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...