GWT Custom Event Handler

57,999

Solution 1

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<HappyEvent,HappyHandler> KEY = new Key<HappyEvent,HappyHandler>(){
    protected void fire(HappyHandler handler, HappyEvent event) {
       handler.onHappiness(event);
    };
   ...
}

Solution 2

Thanks for all the responses. Zakness came the closest to giving me the answer I needed, however, I came up with a slightly simpler model.

My main goal was to avoid using a static variable to my main data structure. I also hit the problem of trying to figure out if that main data structure was successfully retrieved from the database at the time of trying to access it and what to do when it's not (i.e. when it's null).

After watching the Google Web Toolkit Architecture: Best Practices For Architecting Your GWT App video from Google IO, the Event Bus idea seemed perfect.

I'll post my solution here in case it helps anyone else out.


First, create the Handler class. Note the reference to the Event class already:

public interface CategoryChangeHandler extends EventHandler {
    void onCategoryChange(CategoryChangeEvent event);
}

Now on to the Event class. This gave me the most trouble:

public class CategoryChangeEvent extends GwtEvent<CategoryChangeHandler> {

    private final List<Category> category;

    public CategoryChangeEvent(List<Category> category) {
        super();
        this.category = category;
    }

    public static final Type<CategoryChangeHandler> TYPE = new Type<CategoryChangeHandler>();

    @Override
    protected void dispatch(CategoryChangeHandler handler) {
        handler.onCategoryChange(this);
    }

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

    public List<Category> getCategories(){
        return category;
    }

}

Now I am able to use these Handler and Event classes like so when this main data structure gets reloaded:

This code got the data structure and want to notify everyone who is listening that it got updated:

CategoryChangeEvent event = new CategoryChangeEvent(result);
eventBus.fireEvent(event);

This code is an implementation of the Event

public class PopulateCategoryHandler implements CategoryChangeHandler {

    @Override
    public void onCategoryChange(CategoryChangeEvent event) {
        tearDownCategories();

        List<Category> categories = event.getCategories();
        populateCategories(categories); 
    }

}

Solution 3

Here is an example of this over on Alex Reid's blog, including a link to an operational code example. The example fills in some of the fuzzy bits and, along with Nick's example here, helps clarify getting started with architecting an event bus in your gwt application.

Solution 4

I think that the most complete and detailed example is in this article

It contains also an example project that shows exactly how to properly use define custom events and use GWT's HandlerManager class.

Solution 5

One additional comment: if you try do to something similar to react in a main application to an event fired from a custom GUI component (like a composite etc.), I think you have to wire the main app explicitly to handle the component's event:

yourComponent.addHandler(this, YourEvent.TYPE);

where "this" is the class that implements your custom handler interface.

Share:
57,999

Related videos on Youtube

Nick
Author by

Nick

Updated on September 29, 2020

Comments

  • Nick
    Nick over 3 years

    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.

    You create a HandlerManager and now you have to create an Event and a Handler. How would you define those classes so that you can subscribe and fire events?

    Most of the Events are DOM based, while I want to create some custom events and handlers that I can fire outside of any browser-based event.

  • vasu
    vasu almost 15 years
    I hope the example proves useful. :)
  • Eric Nguyen
    Eric Nguyen over 13 years
    Unfortunately, this answer is pretty badly deprecated by now (which the author already anticipated). For a much more comprehensive overview of custom events in GWT, see this more recent question and answer: stackoverflow.com/questions/2951621/gwt-custom-events
  • Ethan Leroy
    Ethan Leroy about 13 years
    it would be great if you could also include the code for creating the eventBus. is it a class written by yourself?
  • Nikita Zhiltsov
    Nikita Zhiltsov almost 13 years
    static EventBus eventBus = GWT.create(SimpleEventBus.class);