SmartGWT Custom Events

Ok, so working with the SmartGWT framework on top of Google Web Toolkit. The goal was to fire a custom even from a ToolStrip when one of its members fired a ClickEvent.

This is relatively easily accomplished by subclassing ToolStrip and implementing the HasDataChangedHandlers interface. The only thing that took a bit of time to figure out was that I needed to use the SmartGWT function doAddHandler in BaseWidget instead of the GWT addHandler method in order to properly register the handler with the HandlerManager

Main Class

package test.client;
 
import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.data.events.DataChangedEvent;
import com.smartgwt.client.data.events.DataChangedHandler;
 
public class CustomEvents implements EntryPoint {
    public void onModuleLoad() {
        MyStrip t = new MyStrip();
        t.addDataChangedHandler(new DataChangedHandler() {
            @Override
            public void onDataChanged(DataChangedEvent event) {
                // Handle the event here
            }
        });
        t.test();
    }
}

Subclassed ToolStrip

package test.client;
 
import com.google.gwt.event.shared.HandlerRegistration;
import com.smartgwt.client.data.events.DataChangedEvent;
import com.smartgwt.client.data.events.DataChangedHandler;
import com.smartgwt.client.data.events.HasDataChangedHandlers;
import com.smartgwt.client.widgets.toolbar.ToolStrip;
 
public class MyStrip extends ToolStrip implements HasDataChangedHandlers {
 
    public void test() {
        fireEvent(new DataChangedEvent(this.getJsObj()));
    }
 
    @Override
    public HandlerRegistration addDataChangedHandler(DataChangedHandler handler) {
        return doAddHandler(handler, DataChangedEvent.getType());
    }
}

One thought on “SmartGWT Custom Events

  1. Thank you! Thank you! Thank you! I tried hooking up my own custom events and couldn’t understand why they weren’t firing. I was using ‘addHandler’. When I switched over to ‘doAddHandler’ everything worked great!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">