GWT — Calling a Java Method from Handwritten JavaScript
For the remotespots project, we developed a notification panel which is very similiar to Stackoverflow notification bar. So, what this bar provides is the a HTML message and a close bottom at the right end.
For the close button, instead of adding a hyperlink from the java code, this time we used JSNI which enables you to integrate JavaScript directly into your application.
We have a class called GenericNotifcation in the package org.remote.dogan.client. When a noticition bar is needed,
public static void showNotification(String message, int seconds)
is called.To hide the bar the following method is called,
public static void hideNotification()
So, to make the JSNI close method call from js, we need to call the hideNotification java method from js.
To do this, we add amethod to register hideNotification method a js method for the handwritten javascript
public static native void exportStaticMethod() /*-{
$wnd.closeNotification =
@org.remote.dogan.client.GenericNotifcation::hideNotification();
}-*/;
As a second step, we should call the method exportStaticMethod() to actually make the registration of the js method. We do this in showNotification method but for a generic case, onModuleLoad is good candidate.
public static void showNotification(String message, int seconds)
{
GenericNotifcation.exportStaticMethod();
...
}
So as the last step, we add the js call in the html template like the following:
<a title=”close this notification” onclick=’closeNotification()’ />x</a>
For further information, the documentation for GWT JSNI is here.





