GWT Custom Event Handler

Can someone give me an example of creating a custom set of events and a handler. Let's say you have a Person object that you want your widgets to know if it has been updated.

A HandlerManager is created, and now you need to create an event and a handler. How would you define these classes so you can subscribe and fire events?

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

+43
java javascript-events events dom-events gwt
Jun 15 '09 at 21:58
source share
8 answers

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

This is an event that fires when a user becomes happy.

Define a new event class. You can add custom metadata to the event class. For simplicity, we will not include here.

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; } ... } 

Connect fire handler method

 class HappyEvent extends GwtEvent { static Key<HappyEvent,HappyHandler> KEY = new Key<HappyEvent,HappyHandler>(){ protected void fire(HappyHandler handler, HappyEvent event) { handler.onHappiness(event); }; ... } 
+23
Jun 19 '09 at 18:02
source share

Thanks for all the answers. Zaknes came closest to give me the answer I needed, however I came up with a slightly simpler model.

My main goal was to avoid using a static variable in my main data structure. I also ran into the problem of trying to find out if this basic data structure was successfully retrieved from the database during an attempt to access it and what to do when it is absent (i.e. when it is zero).

After watching the Google Web Toolkit Architecture: Best Practices for archiving your GWT application from Google IO, the Event Bus idea seemed perfect.

I will post my solution here if this helps someone else.




First create the Handler class. Pay attention to the link to the Event class:

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

Now to the Event class. This gave me a big nuisance:

 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 can use these Handler and Event classes, for example, when this basic data structure is reloaded:

This code has received a data structure and wants to notify everyone who listens that it has been 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); } } 
+31
Jun 21 '09 at 3:49
source share

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

+4
Jul 15 '09 at 18:39
source share

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

It also contains an example project that shows how to properly use specific user events and use the GWT HandlerManager class.

+2
May 28 '10 at 14:07
source share

Creating custom GWT events using HandlerManger shouldn't be that hard; look at the GwtEventBus @ NingZhang.info example , it's real intuitive. The key classes are:

  • com.google.gwt.event.shared.HandlerManager
  • com.google.gwt.event.shared.GwtEvent
  • com.google.gwt.event.shared.EventHandler
+1
Jan 31 '10 at 4:01
source share

One more comment: if you try to do something similar to a reaction in the main application to an event released from the user component of the graphical interface (for example, composite, etc.), I think that you need to directly connect the main application to handle the event component:

 yourComponent.addHandler(this, YourEvent.TYPE); 

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

+1
May 24 '11 at 12:20
source share

It looks like you want PropertyChange * support. Take a look at gwtx . Google 'gwt PropertyChange' and you will get some blogs that explain how to use them.

0
Jun 15 '09 at 22:34
source share

Perhaps you should take a look at ValueChangeHandler and ValueChangeEvent in GWT 1.6. They may work for what you are trying to do.

0
Jun 17 '09 at 19:56
source share



All Articles