Calling JSNI function or Java function to handle onClick event

405 views
Skip to first unread message

DK

unread,
Jun 2, 2010, 3:46:35 PM6/2/10
to Google Web Toolkit
I have an anchor element with an onClick="showAlert();"

in my java file, I have a JSNI function

public static native void showAlert() /*-{
alert('Hello');
}-*/;

I never see this alert. In fact, when I look at the browser console, I
see that showAlert could not be found. What am I missing here ?

kozura

unread,
Jun 2, 2010, 4:44:55 PM6/2/10
to Google Web Toolkit
Java functions are translated into obfuscated (ie renamed) javascript,
so will not be available to call from an element. You can create a
javascript function, perhaps attached to wnd, which could then call a
java function.

Manuel Carrasco Moñino

unread,
Jun 2, 2010, 5:17:17 PM6/2/10
to google-we...@googlegroups.com
Although GWT obfuscates the produced javascript code renaming methods,
classes, etc, it is possible to use a GWT method from javascript if
you export it storing a reference in the window object:
private native void exportMyMethod() /*-{
$wnd.showAlert = function(name) {
return .@com.example.MyClass::showAlert(Ljava/lang/String;) (message);
};
}-*/;

Take a look to this article I wrote time ago:
http://code.google.com/p/gwtchismes/wiki/Tutorial_ExportingGwtLibrariesToJavascript_en

-Manolo

> --
> You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
> To post to this group, send email to google-we...@googlegroups.com.
> To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
>
>

Reply all
Reply to author
Forward
0 new messages