Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied! <p>Since this question and the answer from Piotr GWT has added support for a slightly different way to create custom events. This event implementation is specific build to be used with the GWT's EventBus in the package <code>com.google.web.bindery.event.shared</code>. An example on how to build a custom event for GWT 2.4:</p> <pre class="lang-java prettyprint-override"><code>import com.google.web.bindery.event.shared.Event; import com.google.web.bindery.event.shared.EventBus; import com.google.web.bindery.event.shared.HandlerRegistration; /** * Here is a custom event. For comparison this is also a MessageReceivedEvent. * This event extends the Event from the web.bindery package. */ public class MessageReceivedEvent extends Event&lt;MessageReceivedEvent.Handler&gt; { /** * Implemented by methods that handle MessageReceivedEvent events. */ public interface Handler { /** * Called when an {@link MessageReceivedEvent} event is fired. * The name of this method is whatever you want it. * * @param event an {@link MessageReceivedEvent} instance */ void onMessageReceived(MessageReceivedEvent event); } private static final Type&lt;MessageReceivedEvent.Handler&gt; TYPE = new Type&lt;MessageReceivedEvent.Handler&gt;(); /** * Register a handler for MessageReceivedEvent events on the eventbus. * * @param eventBus the {@link EventBus} * @param handler an {@link MessageReceivedEvent.Handler} instance * @return an {@link HandlerRegistration} instance */ public static HandlerRegistration register(EventBus eventBus, MessageReceivedEvent.Handler handler) { return eventBus.addHandler(TYPE, handler); } private final String message; public MessageReceivedEvent(String message) { this.message = message; } @Override public Type&lt;MessageReceivedEvent.Handler&gt; getAssociatedType() { return TYPE; } public String getMessage() { return message; } @Override protected void dispatch(Handler handler) { handler.onMessageReceived(this); } } </code></pre> <p>The event is used as follows:</p> <p>To register your handler for this event with the eventbus call the static register method on the MessageReceivedEvent class:</p> <pre class="lang-java prettyprint-override"><code>MessageReceivedEvent.register(eventbus, new MessageReceivedEvent.Handler() { public void onMessageReceived(MessageReceivedEvent event) { //...do something usefull with the message: event.getMessage(); } }); </code></pre> <p>Now to fire the event on the eventbus call <code>fireEvent</code> with a newly constructed event:</p> <pre class="lang-java prettyprint-override"><code>eventBus.fireEvent(new MessageReceivedEvent("my message")); </code></pre> <p>Another implementation can be found in GWT's own <code>EntityProxyChange</code> event class. That implementation uses a alternative option of the EventBus. It uses the ability to add handlers that are bound to a specific source, via <code>addHandlerToSource</code> and can be triggered via <code>eventBus.fireEventFromSource</code>.</p> <p>The event implementation given here is also more suitable when working with GWT's <a href="http://code.google.com/webtoolkit/doc/latest/DevGuideMvpActivitiesAndPlaces.html" rel="noreferrer">Activities</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload