CheckboxItem

How do I retrieve the boolean (checked or unchecked) value of a CheckboxItem? I tried .getValue() but I get a javascript error

Uncaught JavaScript exception [_1 has no properties] in http://localhost:8888/....MainEntryPoint/sc/modules/ISC_Core.js, line 736 and

thank you

What are you calling “getValue()” on? If on a form, you need to pass the item’s name. If that didn’t help, please show your code.

I am calling it on the CheckboxItem directly. I am doing this because i have them stored in an array. Its just a really basic check box list.

       private CheckboxItem[] widgetChoices = new CheckboxItem[3];
       widgetChoices[0] = new CheckboxItem("1");
       widgetChoices[1] = new CheckboxItem("2");
       widgetChoices[2] = new CheckboxItem("3");

       private DynamicForm form = new DynamicForm(); 
       form.setFields(widgetChoices);


	public LinkedList<String> getCheckedItem(){
		
		LinkedList<String> toReturn = new LinkedList<String>();
		
	
		for(int i = 0; i < widgetChoices.length; i++){
			
			if((Boolean)widgetChoices[i].getValue() == true){
			       toReturn.add(widgetChoices[i].getName());
			}
		}
		
		return toReturn;
	}

OK, can you post a stack trace for this error, available from Firebug or from the Developer Console in Internet Explorer (both would be great).

Hello:

I wrote a simpler test that shows the error better:

public class MainEntryPoint implements EntryPoint {

	public void onModuleLoad() {

		DynamicForm form = new DynamicForm();
		form.setNumCols(1);	
		CheckboxItem[] list = new CheckboxItem[3];
		
		for(int i = 0; i < list.length; i++){ 
			list[i] = new CheckboxItem(Integer.toString(i));
			list[i].setValue(false);
			System.out.println(list[i].getValue());
			list[i].setRedrawOnChange(true);
		}

		form.setFields(list); 
	}

This code won’t even compile, as GWT is complaining about the CheckboxItem.getValue() method

[ERROR] Unable to load module entry point class test.client.MainEntryPoint (see associated exception for details)
com.google.gwt.core.client.JavaScriptException: (TypeError): self.getValue is not a function
 fileName: jar:file:/home/paperclip/workspace/HDDSS-smartGWT/lib/smartgwt.jar!/com/smartgwt/client/widgets/form/fields/FormItem.java
 lineNumber: 2253
 stack: ()@jar:file:/home/paperclip/workspace/HDDSS-smartGWT/lib/smartgwt.jar!/com/smartgwt/client/widgets/form/fields/FormItem.java:2253
gwtOnLoad([object Window],"test.MainEntryPoint","1.5")@:0
gwtOnLoad((function () {alert("Failed to load module test.MainEntryPoint\".\nPlease see the log in the development shell for details.");}),"test.MainEntryPoint","http://localhost:8888/test.MainEntryPoint/")@http://localhost:8888/test.MainEntryPoint/hosted.html?test_MainEntryPoint:20
maybeStartModule()@http://localhost:8888/test.MainEntryPoint/test.MainEntryPoint.nocache.js:100
()@http://localhost:8888/test.MainEntryPoint/test.MainEntryPoint.nocache.js:354
@http://localhost:8888/test.MainEntryPoint/hosted.html?test_MainEntryPoint:39

	at com.smartgwt.client.widgets.form.fields.FormItem.getValue(Native Method)
	at test.client.MainEntryPoint.onModuleLoad(MainEntryPoint.java:20

Thanks for the testcase. I’ll look into this. The issue here is related to calling of getValue() before the form has been rendered. Can you try running that code in, say, a Button click listener so that it executes after the form has been rendered.

Sanjiv

It works if i put that code into a click handler. Is there a quick fix or a work around for this?

Try

list[i].getAttributeAsBoolean("value");

It seems like if I set the value initially via CheckboxItem.setValue(false) then the error goes away. Accessing the value via CheckboxItem.getValue.equals(true) then becomes possible!

thanks for the help :slight_smile:

i discovered a similar problem (i guess) and solved via calling draw() and destroy() before and after the get value call.

hope it helps!

cheerios