Dims
Dims

Reputation: 51059

How to use ancestor's EventBus if I am extending Widget in GWT?

I am extending a ScrollPanel and wish to process my own custom events with this new widget.

I made my own HasMyHandlers interface with two methods fireEvent(MyEvent event) and HandlerRegistration addMyHandler(MyHandler handler).

First I made a private member SimpleEventBus eventBus, but next thought that the ancestor class should already have it's own copy.

Is it possible to use ancestor's event bus, i.e. to register handlers within it and to fire them according to it?

Upvotes: 1

Views: 163

Answers (1)

Strelok
Strelok

Reputation: 51461

Yes, it is possible. The standard way to do this is in the implementation of HandlerRegistration addMyHandler(MyHandler handler) is :

public class MyWidget extends Widget implements HasMyHandlers {

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

  // Other methods
}

The Widget.addHandler(...) method provides the mechanism to wire custom event handlers to the Widget's HandlerManager.

To fire your event to all registered handlers you use Widget.fireEvent(...) method. So to fire your event you can do:

fireEvent(new MyEvent(yourEventData));

It's worth noting that your event should also extend the GwtEvent class.

Upvotes: 3

Related Questions