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
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
fireEventmethod (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.