简体   繁体   中英

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, ie to register handlers within it and to fire them according to it?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM