GWT Custom Event Handler

后端 未结 8 623
慢半拍i
慢半拍i 2020-12-07 16:44

Can someone give me an example of creating a custom set of an Event and a Handler. Say you have a Person object that you want your widgets to know if it got updated.

8条回答
  •  情话喂你
    2020-12-07 17:16

    Here's a pretty comprehensive example of creating a custom event, taken verbatim from the GwtEventSystem Wiki (when the event system was still in GWT's incubator).

    This is an event that is triggered when the user becomes happy.

    Define a new event class. You can add arbitrary metadata in the event class. For simplicity, we will not include any here though.

    public class HappyEvent extends GwtEvent {
      ...
    }
    

    Define a new handler and marker interface for the event class.

    interface HappyHandler extends EventHandler {
      public void onHappiness(HappyEvent event);
    }
    
    interface HasHappyEvents {
      public HandlerRegistration addHappyHandler(HappyHandler handler);
    }
    

    Add a unique event type

    class HappyEvent extends AbstractEvent{
      public static AbstractEvent.Key KEY = new AbstractEvent.Key(){...}
    
      public GwtEvent.Key getKey(){
        return KEY; 
      }
      ...
    }
    

    Wire up the handler's fire method

    class HappyEvent extends GwtEvent {
      static Key KEY = new Key(){
        protected void fire(HappyHandler handler, HappyEvent event) {
           handler.onHappiness(event);
        };
       ...
    }
    

提交回复
热议问题