Problems retrieving more selected items from a CheckBoxGroup

Hello!

I've one big problem I can't work out: I'm trying to use a CheckBoxGroup to select more than one value from a list of four. I need the changes to be auto-submitted. The problem is that after the data is submitted to the server and comes back to the browser, only one item is selected, namely the previously selected one lower in the page. Debugging I noticed that calling getSelected() on this checkbox group always gives an array of length 1! The same happens when I don't auto-submit the values.

Here is what I've done:

In SessionBean1:

....

private Option[] availabilitiesOptions;

private String[] availabilitiesSelected;

public SessionBean1(){

.....

availabilitiesOptions=new Option[]{// Values are taken from byte constants

new Option(new Byte(AvailabilityType.UNLICENSED).toString(),

"Contacts without licenses"),

new Option(new Byte(AvailabilityType.LICENSED).toString(),

"Contacts with licenses"),

new Option(new Byte(AvailabilityType.PARTNER).toString(),

"Partners"),

new Option(new Byte(AvailabilityType.ADMIN).toString(),

"Administrators")

};

availabilitiesSelected=new String[]{};

}

public Option[] getAvailabilitiesOptions(){

return this.availabilitiesOptions;

}

publicvoid setAvailabilitiesOptions(Option[] availabilitiesOptions){

this.availabilitiesOptions = availabilitiesOptions;

}

public String[] getAvailabilitiesSelected(){

return this.availabilitiesSelected;

}

publicvoid setAvailabilitiesSelected(String[] availabilitiesSelected){

this.availabilitiesSelected = availabilitiesSelected;

}

In 'Design' of the web page:

I added a CheckBoxGroup, set it to 'auto-submit on change' and set immediate to true;

I bound the data to #{SessionBean1.availabilitiesOptions} choosing from the 'Bind to an object' panel. I set the 'selected' property to #{SessionBean1.availabilitiesSelected} from the Properties panel

I then double-clicked the checkBoxGroup to add the following code:

publicvoid availabilityCheckBoxes_processValueChange(ValueChangeEvent event){

String[] selection=(String[])availabilityCheckBoxes.getSelected();

.....

// The following line is to avoid validation of other required components

FacesContext.getCurrentInstance().renderResponse();

}

Debugging, selection

has always only one item!!

I think this is all I've done concerning this issue.

I hope I'm doing something wrong and it's not an IDE bug, but please help me find out where's the problem!!

Thanks

Davide

PS: I'm using version 060120 on Windows XP

[4124 byte] By [davide] at [2007-11-26 7:07:01]
# 1

I forgot to say that sometimes, after the first time I select a checkbox, if I select also one which is higher in the page (i.e. one which would not appear in the list of the selected items), the other components on the page get validated, although I specified the checkboxgroup as 'immediate'!

Thanks!

davide

davide at 2007-7-6 15:54:48 > top of Java-index,Development Tools,Java Tools...
# 2

Hello,

I duplicated your project (as far as the checkboxes go).

Here is the process value change code that I used:

public void checkboxGroup1_processValueChange(ValueChangeEvent event) {

log("start process value change");

String[] selection=(String[])checkboxGroup1.getSelected();

for (int i = 0; i < selection.length; i++) { // add new items to the list

log("PVC: Selected: " + selection[i]);

}

log("end process value change");

// The following line is to avoid validation of other required components

FacesContext.getCurrentInstance().renderResponse();

}

I do not see the problem that you are seeing. That is, selected has a length bigger than 1.

However, by calling renderResponse() at this phase, you are making the app bypass the phase where copies selected to sessionBean1.availabilitiesSelected.

Rather than call renderResponse to bypass validation of other required components, perhaps you might try putting the check box in a virtual form.

For more information, see http://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/vir tual_form.html and http://developers.sun.com/prodtech/javatools/jscreator/reference/techart/2/virt ual_forms.html.

jetsons at 2007-7-6 15:54:48 > top of Java-index,Development Tools,Java Tools...
# 3

Thank you very much for your reply. So you say your 'selected' array has more than one item. I'm sure this doesn't happen to me, so maybe the problem concerns my environment. Anyway, I'll try using a virtual form as you suggest, maybe the behaviour will change! Thank you very much again!

Davide

davide at 2007-7-6 15:54:48 > top of Java-index,Development Tools,Java Tools...
# 4

> Thank you very much for your reply. So you say your

> 'selected' array has more than one item. I'm sure

> this doesn't happen to me, so maybe the problem

> concerns my environment.

So, you are saying that you removed the following statement and selected still only had one item?

// The following line is to avoid validation of other required components

FacesContext.getCurrentInstance().renderResponse();

Because as long as you have that statement, you will have only one item in your array.

jetsons at 2007-7-6 15:54:48 > top of Java-index,Development Tools,Java Tools...
# 5

Yes, right: I removed that line, put the component into a virtual form and the behaviour is the same as before! Since you don't have the wrong behaviour I have, I think I did something stupid: I'll go through it once more, step by step and as thoroughly as possible, and if I can't fix anything I'll change the whole approach! Thank you very much for your help!

Davide

davide at 2007-7-6 15:54:48 > top of Java-index,Development Tools,Java Tools...