Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I created my own widget by extending GWT's Composite class. I wanted to create my own custom event in this class. I wanted the events to be accessible to GWT's WindowBuilder Editor.</p> <p>I learned a lot of from the answers on this page, but I had to make some changes.</p> <p>I wanted to start from the Hilbrand Bouwkamp answer, because it was newer. But I ran into a couple of problems. 1) That answer made reference to the event bus. The even bus is a global variable owned by the main program. It's not clear how a widget library could get access to that. 2) I wasn't starting from scratch. I was was extending GWT library code. In order to make that work, I had to start from the GwtEvent class, rather than the Event class.</p> <p>Piotr's answer is essentially correct, but it was very long. My class (indirectly) extends GWT's Widget class. Widget takes care of many details, such as creating a HandlerManager object. (I looked through the source code, and that's exactly how standard Widgets work, not by using an EventBus.)</p> <p>I only had to add two things to my widget class to add a custom event handler. Those are shown here:</p> <pre><code>public class TrackBar extends Composite { public HandlerRegistration addValueChangedHandler(TrackBarEvent.Handler handler) { return addHandler(handler, TrackBarEvent.TYPE); } private void fireValueChangedEvent() { final TrackBarEvent e = new TrackBarEvent(value); fireEvent(e); } </code></pre> <p>My new event is almost exactly the same as Piotr's event class, shown above. One thing is worth noting. I started with getValue(), based on that example. Later I added getTrackBar() to give a lot more information. If I was starting from scratch I'd focus on the latter, not the former. The complete event class is shown below.</p> <pre><code>import com.google.gwt.event.shared.EventHandler; import com.google.gwt.event.shared.GwtEvent; public class TrackBarEvent extends GwtEvent&lt; TrackBarEvent.Handler &gt; { public interface Handler extends EventHandler { void onTrackBarValueChanged(TrackBarEvent event); } static final Type&lt;TrackBarEvent.Handler&gt; TYPE = new Type&lt;TrackBarEvent.Handler&gt;(); private final int value; public TrackBarEvent(int value) { this.value = value; } @Override public Type&lt;TrackBarEvent.Handler&gt; getAssociatedType() { return TYPE; } public int getValue() { return value; } public TrackBar getTrackBar() { return (TrackBar)getSource(); } @Override protected void dispatch(Handler handler) { handler.onTrackBarValueChanged(this); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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