Maxime ARNSTAMM
Maxime ARNSTAMM

Reputation: 5314

GWT Events : what am i missing?

I am trying to send an object through the gwt event bus and i don't know why it doesn't work.

Usually, i have a component A which creates a popup. A registers to the popup, and the popup fires the event. Now, the listener (OtherComponent) isn't related to the popup. When the popup fires the event, the other compoment doesn't catch it.

Here's my code :

Handler:

public interface MyEventHandler extends EventHandler {
   public void onChanged(MyEvent event);
}

Event :

public class MyEvent extends GwtEvent<MyEventHandler> {
  private static final GwtEvent.Type<MyEventHandler> TYPE = new GwtEvent.Type<MyHandler>();

  private MyBean my;

  public MaterielEvent(My bean) {
    my = bean;
  }

  @Override
  public com.google.gwt.event.shared.GwtEvent.Type<MyEventHandler> getAssociatedType() {
    return TYPE;
  }

  public static Type<MyEventHandler> getType() {
    return TYPE;
  }

  @Override
  protected void dispatch(MyEventHandler handler) {
    handler.onChanged(this);
  }

  public MyBean getBean() {
    return my;
  }
}

Component :

public class OtherPanel extends Composite implements HasMyEventHandlers {
  interface OtherPanelUiBinder extends UiBinder<Widget, OtherPanel> {}
  private static OtherPanelUiBinder uiBinder = GWT.create(OtherPanelUiBinder.class);

  public OtherPanel() {
    this.addMyEventHandler(new MyEventHandler() {
       @Override
       public void onChanged(MyEvent event) {
         NotificationManager.success("event recieved");
       }
    });

    initWidget(uiBinder.createAndBindUi(this));
  }

  @Override
  public HandlerRegistration addMyEventHandler(MyEventHandler handler) {
    return addHandler(handler, MyEvent.getType());
  }
}

Call (inside another component) :

  fireEvent(new MyEvent(myBean));

The notification "event received" is never called.

I surely missed something. Thanks for your help

Upvotes: 1

Views: 1317

Answers (2)

Tomasz Gawel
Tomasz Gawel

Reputation: 8510

As @Jeff Allen wrote. Each widget has its private EventBus instance (it is not static). if your events are not instance specific you can use EventBus directly not via Widgets wrapper methods. Your Event and Handler classes are perfectly ready for this.

See javadoc at http://google-web-toolkit.googlecode.com/svn/javadoc/2.4/com/google/gwt/event/shared/EventBus.html (as you noticed Widgets event methods are simply wrappers over evtnbus methods.

Upvotes: 2

Jeff Allen
Jeff Allen

Reputation: 17517

You're creating a new instance of an EventBus and defining the eventHandler on that new instance.

I can't see how you're calling the fireEvent method (or from what instance of the eventBus you are calling it), but you need to have a single eventBus instance defined in your code which you pass around.

So you instantiate an eventBus, then define any handlers you want it to have, and then have any components which will interact with eventBus accept a "MyEventHandler" as a parameter to the constructor. Then you can pass your pre-defined instance of an event bus into that component, allowing that component to later interact with the singular eventBus that your application has.

Upvotes: 5

Related Questions