PDA

View Full Version : How to get Value of CheckBox in GXT



hossaindoula
27 Jan 2010, 12:23 PM
I have a CheckBoxGroup and under that group I have multiple CheckBoxes... How can I get the checked value(s)??? I have done something like this:





final CheckBoxGroup chkGrp = new CheckBoxGroup();
chkGrp.setFieldLabel("Section");
CheckBox chkBxFld = new CheckBox();
chkBxFld.setBoxLabel("Family Section");
CheckBox chkBxFld1 = new CheckBox();
chkBxFld1.setBoxLabel("Single Section");
chkGrp.add(chkBxFld);
chkGrp.add(chkBxFld1);
fieldSet.add(chkGrp);

String family_single = "";
List<CheckBox> family_single_check_list = chkGrp.getValues();
Iterator itr = family_single_check_list.iterator();
while(itr.hasNext()){
Object element = itr.next();
family_single += element.toString() + "/";
}



The problem I am facing is the in the string varible "family_single" the CheckBox widget is also coming... Why the CheckBox comes inside a String??? Is there any other way to return the checked value of CheckBoxGroup in EXT-GWT(GXT)???

tortexy
28 Jan 2010, 5:59 AM
You may check the sample:
http://www.extjs.com/examples/explorer.html#gridbinding

it does bind ModelData through FormBinding to a Form.

It works the same way for checkboxes too.

Arno.Nyhm
29 Jan 2010, 11:57 AM
Why the CheckBox comes inside a String??? Is there any other way to return the checked value of CheckBoxGroup in EXT-GWT(GXT)???

in your iterator you use object instead of casting it to checkbox ... that was the problem i think.



18514

look at this code:



CheckBox check1 = new CheckBox();
check1.setBoxLabel("Brochures");
check1.setValueAttribute("Bro");

CheckBox check2 = new CheckBox();
check2.setBoxLabel("Events");
check2.setValue(true);
check2.setValueAttribute("Ev");

CheckBox check3 = new CheckBox();
check3.setBoxLabel("Announcements");
check3.setValue(true);
check3.setValueAttribute("Ann");

final CheckBoxGroup checkGroup = new CheckBoxGroup();
checkGroup.setFieldLabel("Subscribe");
checkGroup.add(check1);
checkGroup.add(check2);
checkGroup.add(check3);
fp.add(checkGroup);


Button click = new Button("click");
click.addSelectionListener(new SelectionListener<ButtonEvent>() {

@Override
public void componentSelected(ButtonEvent ce) {

String info = "";
List<CheckBox> values = checkGroup.getValues();
for (CheckBox checkBox : values) {
info += "<br>" + checkBox.getBoxLabel() + ": " + checkBox.getValueAttribute() + " " + checkBox.getValue();
}

MessageBox.alert("title", info, null);
}
});

hossaindoula
2 Feb 2010, 3:50 AM
It is working... Thanx Arno.Nyhm... :)

ashly.jos@gmail.com
26 Apr 2011, 2:02 AM
hi,

I got this working!! But, how do I iterate through all the checkboxes in the checkboxgroup?

Thanks,
-ash

micgala
26 Apr 2011, 2:56 AM
??

Haven't you seen the code above?



List<CheckBox> values = checkGroup.getValues();
for (CheckBox checkBox : values) {

ashly.jos@gmail.com
26 Apr 2011, 5:08 AM
Hi micgala,

Thanks for your comment.

But, this part of code gives you only selected checkboxes, not the unselected ones.

Anyways, I have figured out how to get all the checkboxes. Here is the code.



List<Field<?>> fieldList = checkBoxGroup.getAll();
for(Field<?> fields : fieldList){
if(fields instanceof CheckBox){
CheckBox checkBox = (CheckBox) fields;
}
}


Cheers,
-ash