Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    setValue does not fire events

    Hi. I've got a problem with form fields. I've added changed listener to selecItem field. When I select value from drop down list event is fired. But when I do setValue on selectItem field, value changes, but event is not fired :/
    Do I have to fire this event manually after invoking setValue on field? If so could you give a code snippet showing this.

    #2
    Please... Does anybody know how to solve it?
    There's a constructor in ChangedEvent class which takes JavaScriptObject as argument, but I just don't know how to use it :/
    Last edited by mtus; 21 Nov 2010, 23:46.

    Comment


      #3
      mtus, try this code to send the event

      Code:
      	private native void sendOnChangeEvent(DynamicForm form, String formItemName, String oldValue, String newValue) /*-{
      		var formWidget = form.@com.smartgwt.client.widgets.form.DynamicForm::getOrCreateJsObj()();
      		var formItem = formWidget.getField(formItemName);
      		if (typeof formItem.change == "function") {
      			formItem.change(formItem.form, formItem, newValue, oldValue);
      		} 
      	}-*/;
      Do not forget to set value after event is sent.
      Code:
      	sendOnChangeEvent(myForm, formItem.getName(), oldValue, newValue);
      	formItem.setValue(newValue);
      P.S. not sure how legal is this but it does what I need. Good luck.

      Comment


        #4
        Hey hsm,
        Can you tell me where should i put these codes?

        Comment


          #5
          RAS,
          the first one is the native method which sends the event. You can put it anywhere. Make it public if you going to use it more than in one place. How it works - look here http://googlewebtoolkit.blogspot.com/2008/07/getting-to-really-know-gwt-part-1-jsni.html

          the second part is a sample call to this method and value assignment.

          Comment


            #6
            Hey hsm,
            Thanks for your fast reply but I've done exactly the same as you mentioned. But still I'm not able to get it working. If possible, can you please post a stand alone code?
            Thanks.

            Comment


              #7
              RAS,
              Check this out.
              Code:
              package demo.client;
              
              import com.google.gwt.core.client.EntryPoint;
              import com.google.gwt.user.client.ui.RootPanel;
              import com.smartgwt.client.util.SC;
              import com.smartgwt.client.widgets.form.DynamicForm;
              import com.smartgwt.client.widgets.form.fields.TextItem;
              import com.smartgwt.client.widgets.form.fields.events.ChangeEvent;
              import com.smartgwt.client.widgets.form.fields.events.ChangeHandler;
              
              public class Demo implements EntryPoint {
              	public void onModuleLoad() {
              		DynamicForm myForm = new DynamicForm();
              		TextItem textItem = new TextItem("textItem", "TextItem");
              		myForm.setItems(textItem);
              		textItem.addChangeHandler(new ChangeHandler() {
              			public void onChange(ChangeEvent event) {
              				SC.say("Got change event: oldvalue=" + event.getOldValue()+ 
              						" newValue=" + event.getValue());
              			}
              		});
              		RootPanel.get().add(myForm);
              		
              		//here set value without sending event
              		textItem.setValue("TheOldValue");
              		
              		String oldValue = (String) textItem.getValue();
              		String newValue = "TheNewValue";
              
              		//now sending event and setting value
              		sendOnChangeEvent(myForm, textItem.getName(), oldValue, newValue);
              		textItem.setValue(newValue);
              	}
              	
              	private native void sendOnChangeEvent(DynamicForm form, String formItemName, String oldValue, String newValue) /*-{
              		var formWidget = form.@com.smartgwt.client.widgets.form.DynamicForm::getOrCreateJsObj()();
              		var formItem = formWidget.getField(formItemName);
              		if (typeof formItem.change == "function") {
              			formItem.change(formItem.form, formItem, newValue, oldValue);
              		} 
              	}-*/;
              }
              If this not helps try to describe what exactly you are trying to do.

              Comment


                #8
                Thanks for the answer, but I've managed to do it myself. My approach is also based on native method. Basically I've extended TextItem and I've overrode setValue method. Here's the sample code:

                Code:
                public class ConfigurableTextItem extends TextItem {
                
                	private native void fireChangedEvent() /*-{
                		var obj = null;
                		obj = this.@com.smartgwt.client.core.DataClass::getJsObj()();
                		var selfJ = this;
                		if(obj.getValue === undefined){
                			return;
                		}
                		var param = {"form" : obj.form, "item" : obj, "value" : obj.getValue()};
                		var event = @com.smartgwt.client.widgets.form.fields.events.ChangedEvent::new(Lcom/google/gwt/core/client/JavaScriptObject;)(param);
                		selfJ.@com.smartgwt.client.core.DataClass::fireEvent(Lcom/google/gwt/event/shared/GwtEvent;)(event);
                	}-*/;
                
                	@Override
                	public void setValue(String value) {
                		super.setValue(value);
                		fireChangedEvent();
                	}
                
                }

                Comment


                  #9
                  mtus,
                  cool solution, but I avoid using inheritance with form items. That caused tonns of event handling problems in smartGWT 1.6. Not sure everything is better today.

                  Comment


                    #10
                    So far everything works as one would expect, at least in my project.

                    Comment

                    Working...
                    X