Tuesday, July 29, 2025

Mixing Elemental2 with GWT Widgets

I've switched to use Elemental2 for a few things.  The rest of my app still uses the standard GWT widgets.

I can bind directly to the Elemental2 elements from the GWT UI Binder, however, if I programatically want to add an Elemental2 element to a GWT Widget, there doesn't seem to be a direct way to do this.

Eg: How would you add the Elemental2 HTMLCanvasElement, to a GWT SimplePanel using its setWidget method?

To work around this, I created an "Elemental2Widget":

import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.ui.Widget;
import jsinterop.base.Js;

public class Elemental2Widget<T> extends Widget {
  public Elemental2Widget(T elemental2Widget) {
    Element element = Js.cast(elemental2Widget);
    setElement(element);
  }
  public T getElemental2Widget() {
    return Js.cast(getElement());
  }
}

Now I can call it like this:

simplePanel.setWidget(new Elemental2Widget<>(htmlCanvasElement));

Is this how people mix Elemental2 with GWT widgets?  Or is there a better way?

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/google-web-toolkit/47b8c5cd-c1ef-496e-a75e-a552dd21c1ccn%40googlegroups.com.

No comments:

Post a Comment