PDA

View Full Version : Getting the checkbox status of a fieldset



ti-guru
7 Feb 2010, 7:12 AM
Hey,

I've got a form with two fieldsets. Each fieldset has a checkbox to toggle whether info from that fieldset will be sent in the request to the server. Does anyone know how to access the status of the checkbox in the fieldset? Essentially what I'm missing is a "fieldset.isChecked()" function.




private static final int WIDTH = 400;

private FormPanel getForm(String header) {

boolean collapsible = false;
boolean checkBoxToggle = true;
boolean editable = false;
boolean allowBlank = false;

//Init the form panel
FormPanel temp = new FormPanel();
temp.setFrame(false);
temp.setBorders(false);
temp.setBodyBorder(false);
temp.setHeaderVisible(false);
temp.setWidth(WIDTH);

//Init the fieldset
FieldSet field = new FieldSet();
field.setHeading(header);
field.setCheckboxToggle(checkBoxToggle);
field.setCheckboxName(header);
field.setCollapsible(collapsible);

//Make another fieldset with the same code as above.

//Add widgets to the fieldsets.

//Add fieldset to the form.
temp.add(field);

return temp;

}

micgala
8 Feb 2010, 1:02 AM
Hi.

Try the isExpanded method.

If that does not help, you can do like this:



private boolean checked = false;
...

fieldSet.addListener(Events.Collapse, new Listener<FieldSetEvent>() {
public void handleEvent(FieldSetEvent be) {
checked = false;
}
});
fieldSet.addListener(Events.Expand, new Listener<FieldSetEvent>() {
public void handleEvent(FieldSetEvent be) {
checked = true;
}
});
This way, you can use the checked class variable.

Regards,
Michel.

ti-guru
8 Feb 2010, 4:01 AM
Thanks for the advice; this works great when


boolean collapsible = true;
field.setCollapsible(collapsible);

But, if I configure the fieldset to be non-collapsible, this doesn't work. Any ideas on how to work around that problem, e.g getting the check box status when the fieldset is set to be permanently expanded?

ti-guru
8 Feb 2010, 4:22 AM
Here's how the JS guys solved (http://www.extjs.com/forum/showthread.php?t=54282) it. Would also be great to be able to programatically check/uncheck the checkbox (e.g. "field.setChecked(boolean)"). Also, adding an eventlistener like "checkChanged" would make things easier.

I'm amazed that these functions aren't already in the code as many users have urged for them earlier :-?

Arno.Nyhm
10 Feb 2010, 9:10 AM
you can extend the FieldSet into a subclass and make get function to this private Field via JSNI:




import com.google.gwt.dom.client.InputElement;
...
private InputElement checkbox;



PS:
look here how you can do this with JSNI:
http://www.extjs.com/forum/showthread.php?p=427799#post427799

ti-guru
10 Feb 2010, 5:02 PM
Thanks! This solved the problem of getting the checkbox value!



import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.google.gwt.dom.client.InputElement;

public class ExtendedFieldSet extends FieldSet {

public ExtendedFieldSet() {
super();
}

private native InputElement getCheckBox() /*-{
return this.@com.extjs.gxt.ui.client.widget.form.FieldSet::checkbox;
}-*/;



public boolean isChecked() {
InputElement checkbox = getCheckBox();
return checkbox.isChecked();
}

}


Here's how you'll use the ExtendedFieldSet class:


boolean collapsible = false;
boolean expand = true;
boolean checkBoxToggle = true;
boolean editable = false;
boolean allowBlank = false;
boolean checked = false;

final ExtendedFieldSet field = new ExtendedFieldSet();
field.setHeading(header);
field.setCheckboxToggle(checkBoxToggle);
field.setCheckboxName(header);
field.setCollapsible(collapsible);

FormData data = new FormData("-20");

//Create widgets for the fieldset
final SimpleComboBox<String> format = new SimpleComboBox<String>();
format.add("MS Office Excel 2007 (*.xlsx)");
format.add("MS Office Excel 1997-2003 (*.xls)");
format.add("Mathworks binary matlab (*.mat)");
format.add("Tab delimited text file (*.txt)");
format.setEmptyText("Select a file format");
format.setFieldLabel("File format");
format.setName("statFormat");
format.setTriggerAction(TriggerAction.ALL);
format.setWidth(CHECKBOX_WIDTH);
format.setEditable(editable);
field.add(format, data);

//Field listeners
field.addListener(Events.OnClick, new Listener<FieldSetEvent>() {
@Override
public void handleEvent(FieldSetEvent be) {

if (field.isChecked()) {

format.enable();
format.setAllowBlank(!field.isChecked());

} else {

format.disable();
format.setAllowBlank(!field.isChecked());
format.clearInvalid();

}

}
});

ti-guru
11 Feb 2010, 9:18 AM
Hmm. Still struggeling to access the HTML input element and check/uncheck this item (and not the form controller). Right now I'm able to access the form controller with the JSNI method below, but not the visual checked-ness of the box. How do I change the visual check state of the box?

ExtendedFieldSet class



private native void setCheckBox(boolean checked) /*-{
this.@com.extjs.gxt.ui.client.widget.form.FieldSet::checkbox = checked;
}-*/;

public void setChecked(boolean checked) {
setCheckBox(checked);
this.fireEvent(Events.OnClick);
}

ti-guru
11 Feb 2010, 9:53 AM
Doing this:




private native void setCheckBox(boolean checked) /*-{
this.@com.extjs.gxt.ui.client.widget.form.FieldSet::checkbox = checked;
var value = this.@com.extjs.gxt.ui.client.widget.form.FieldSet::checkbox;
value.@com.google.gwt.dom.client.InputElement::setDefaultChecked(Z)(checked);
}-*/;



Causes this:


18:39:00.705 [WARN] [downloadcenter] Member 'setDefaultChecked(Z)' in JSNI reference '@com.google.gwt.dom.client.InputElement::setDefaultChecked(Z)' could not be found; expect subsequent failures
java.lang.NoSuchFieldError: setDefaultChecked(Z)
at com.google.gwt.dev.shell.CompilingClassLoader$DispatchClassInfoOracle.getDispId(CompilingClassLoader.java:169)
at com.google.gwt.dev.shell.CompilingClassLoader.getDispId(CompilingClassLoader.java:930)
at com.google.gwt.dev.shell.Jsni$JsSourceGenWithJsniIdentFixup.visit(Jsni.java:170)
at com.google.gwt.dev.js.ast.JsInvocation.traverse(JsInvocation.java:64)
at com.google.gwt.dev.js.ast.JsVisitor.doTraverse(JsVisitor.java:446)
at com.google.gwt.dev.js.ast.JsVisitor.doAccept(JsVisitor.java:421)
at com.google.gwt.dev.js.ast.JsVisitor.accept(JsVisitor.java:97)
at com.google.gwt.dev.js.JsToStringGenerationVisitor.visit(JsToStringGenerationVisitor.java:379)
at com.google.gwt.dev.js.ast.JsExprStmt.traverse(JsExprStmt.java:37)
at com.google.gwt.dev.js.ast.JsVisitor.doTraverse(JsVisitor.java:446)
at com.google.gwt.dev.js.ast.JsVisitor.doAccept(JsVisitor.java:421)
at com.google.gwt.dev.js.ast.JsVisitor.accept(JsVisitor.java:97)
at com.google.gwt.dev.js.JsToStringGenerationVisitor.printJsBlock(JsToStringGenerationVisitor.java:873)
at com.google.gwt.dev.js.JsSourceGenerationVisitor.visit(JsSourceGenerationVisitor.java:48)
at com.google.gwt.dev.js.ast.JsBlock.traverse(JsBlock.java:43)
at com.google.gwt.dev.js.ast.JsVisitor.doTraverse(JsVisitor.java:446)
at com.google.gwt.dev.js.ast.JsVisitor.doAccept(JsVisitor.java:421)
at com.google.gwt.dev.js.ast.JsVisitor.accept(JsVisitor.java:97)
at com.google.gwt.dev.shell.Jsni.generateJavaScriptForHostedMode(Jsni.java:253)
at com.google.gwt.dev.shell.Jsni.getJavaScriptForHostedMode(Jsni.java:240)
at com.google.gwt.dev.shell.ModuleSpaceOOPHM.createNativeMethods(ModuleSpaceOOPHM.java:46)
at com.google.gwt.dev.shell.CompilingClassLoader.injectJsniMethods(CompilingClassLoader.java:1214)
at com.google.gwt.dev.shell.CompilingClassLoader.findClass(CompilingClassLoader.java:1039)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.google.gwt.dev.shell.ModuleSpace.loadClassFromSourceName(ModuleSpace.java:580)
at com.google.gwt.dev.shell.ModuleSpace.onLoad(ModuleSpace.java:348)
at com.google.gwt.dev.shell.OophmSessionHandler.loadModule(OophmSessionHandler.java:185)
at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:380)
at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:222)
at java.lang.Thread.run(Unknown Source)


Help!

Arno.Nyhm
12 Feb 2010, 12:43 PM
private native void setCheckBox(boolean checked) /*-{
this.@com.extjs.gxt.ui.client.widget.form.FieldSet::checkbox = checked;
var value = this.@com.extjs.gxt.ui.client.widget.form.FieldSet::checkbox;
value.@com.google.gwt.dom.client.InputElement::setDefaultChecked(Z)(checked);
}-*/;




maybe small typo?

setDefaultChecked(Z) -> setDefaultChecked(checked)

ti-guru
13 Feb 2010, 1:49 AM
private native void setCheckBox(boolean checked) /*-{
this.@com.extjs.gxt.ui.client.widget.form.FieldSet::checkbox = checked;
var value = this.@com.extjs.gxt.ui.client.widget.form.FieldSet::checkbox;
value.@com.google.gwt.dom.client.InputElement::setDefaultChecked(checked);
}-*/;


Gave this error message:

10:44:19.990 [ERROR] [downloadcenter] Line 24: Expected a valid parameter type signature in JSNI method reference

JSNI basics (http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsJSNI.html#methods-fields)
JSNI reference card (http://java.sun.com/j2se/1.4.2/docs/guide/jni/spec/types.html#wp16432)

ti-guru
14 Feb 2010, 5:53 AM
private native void setCheckBox(boolean checked) /*-{
this.@com.extjs.gxt.ui.client.widget.form.FieldSet::checkbox = checked;
var value = this.@com.extjs.gxt.ui.client.widget.form.FieldSet::checkbox;
this.@com.google.gwt.dom.client.InputElement::setDefaultChecked(checked);
}-*/;


Returns this error message on load:



14:52:02.373 [ERROR] [downloadcenter] Line 24: Expected a valid parameter type signature in JSNI method reference


Basic JSNI in GWT (http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsJSNI.html#methods-fields)
JNI types (http://java.sun.com/j2se/1.4.2/docs/guide/jni/spec/types.html#wp16432)

Arno.Nyhm
16 Feb 2010, 7:21 AM
private native void setCheckBox(boolean checked) /*-{
this.@com.extjs.gxt.ui.client.widget.form.FieldSet::checkbox = checked;
var value = this.@com.extjs.gxt.ui.client.widget.form.FieldSet::checkbox;
value.@com.google.gwt.dom.client.InputElement::setDefaultChecked(Z)(checked);
}-*/;

in this code you try use setDefaultChecked on the value but you set it before with a boolean. but i think you should get a reference to the element and on this you should do the methods.


maybe you mix something there.


but i would do it in a other way (because its more easy because you get hints from your IDE):

just use JSNI to write getter to get access to the elements.
but calculations, set values etc i would do in java.