Since you need ClickEvent to switch to ClickHandler.onClick , and getting it from JavaScript creates a problem (AFAICT) - I would go with a slightly different, more "general" approach:
Create a simple callback interface:
public interface Callback { void execute(); }
Then you can pass it directly and call it like this:
public native void addClickHandler(Callback callback) ;
Or create an intermediate step:
public void addClickHandler(Callback callback) { _addClickHandler(getCallback(callback)); } private native void _addClickHandler(JavaScriptObject callback) ;
And of course you would use it like this:
SVGRect svg = getMeSVGRect(); svg.addClickHandler(new Callback() { @Override public void execute() {
Personally, I prefer the second solution - while this is more code, I like to keep my own / JSNI methods private (unless it's an overlay object or something like that), and the code is more readable and less error prone (no need to use funky syntax for calling Java functions from within native / JSNI code).
source share