Monday, August 17, 2015

Calling GWT Function from JS in GWT Application

Calling GWT Function from JS in GWT Application
In our *.html file add this line of code

<script type="text/javascript" language="javascript">
 function localJavaScript() {
  var msg = window.gwtJSCall("SiddharthaDhumale");
  alert(msg);
 }
</script>

       <h1>Calling GWT Function from JS in GWT Application</h1>

       <table align="center">
              <tr>
                    
              </tr>
              <tr>
                     <button onclick="localJavaScript();">Button to execute Java function.</button>
              </tr>
              <tr>
                     <td colspan="2" style="color: red;" id="errorLabelContainer"></td>
              </tr>
       </table>
</body>
</html>
In our client side use this code

       public class TestExternalJS implements EntryPoint {
      
        public static String GWTClassFunction(String name) {
                return "HellGWTClassFunction called  " + name + "!";
               }

               public static native void exportMethod() /*-{
                $wnd.gwtJSCall = function(name) {
                 return @com.test.client.TestExternalJS::GWTClassFunction(Ljava/lang/String;)(name);
                };
               }-*/;
      
       /**
        * This is the entry point method.
        */
       public void onModuleLoad() {
              exportMethod();
             
             
       }
}




No comments: