Hi,

I'm currently trying to call a "java" method from a javascript that lives
outside of the generated javascript from GWT.

What I want to do is the following :
  make the gwt component communicates with a javascript component (in both
ways).
I have to say that I don't know at all javascript......

The problem I encounter is the following :
  * call to javascript method (generated by GWT) from my handwritten
javascript does not work : firebug says : $wnd does not exist.
I thought this variable was always existing in javascript but this
assumption is may be false.

I think it is quite basic (probably a problem of understanding in
javascript). I have read the following (this is where I found most of the
code for doing the stuff) :
http://www.gwtapps.com/doc/html/com.google.gwt.doc.DeveloperGuide.JavaScriptNativeInterface.JavaFromJavaScript.html
plus others (like
http://www.mail-archive.com/google-web-toolkit@googlegroups.com/msg38714.html)
but I'm stuck there.

Here the very basic code to test the feature :

============================
       The GWT Component
============================
package test.app.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.dom.client.DivElement;
import com.google.gwt.dom.client.Document;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class GwtAndJavascript implements EntryPoint {

  JsButton jsButton;

  public void onModuleLoad() {
    final Button gwtButton = new Button("Add item to JS !");

    final VerticalPanel panel = new VerticalPanel();
    panel.add(gwtButton);

    RootPanel.get("sendButtonContainer").add(panel);
    MyUtilityClass.exportStaticMethod();


    final ClickHandler handler = new ClickHandler() {

      public void onClick(ClickEvent event) {
        jsButton.callSayHello("User");
      }
    };
    gwtButton.addClickHandler(handler);

    // Now I want to add javascript button that lives in a js script...
    jsButton = new JsButton();
    panel.add(jsButton);

  }

  private class JsButton extends Widget {

    public JsButton() {
      DivElement element = Document.get().createDivElement();
      makeMyGraphicalObject(element);
      setElement(element);
    }

    private native void makeMyGraphicalObject(DivElement element) /*-{
      $wnd.createButton(element);
    }-*/;

    public native void callSayHello(String name) /*-{
      $wnd.javascriptSayHello(name);
    }-*/;

  }

}

=======================
The class that should help to trigger the java method from javascript
=======================
package test.app.client;

import com.google.gwt.user.client.Window;

public class MyUtilityClass {

  public static void sayHello() {
    Window.alert("Say Hello !");
  }

    public static native void exportStaticMethod() /*-{
      $wnd.sayHello = @test.app.client.MyUtilityClass::sayHello();
    }-*/;

}

====================
The HTML page
====================

<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">

    <!--                                                               -->
    <!-- Consider inlining CSS to reduce the number of requested files -->
    <!--                                                               -->
    <link type="text/css" rel="stylesheet" href="GwtAndJavascript.css">

    <!--                                           -->
    <!-- Any title is fine                         -->
    <!--                                           -->
    <title>Web Application Starter Project</title>

    <!--                                           -->
    <!-- This script loads your compiled module.   -->
    <!-- If you add any GWT meta tags, they must   -->
    <!-- be added before this line.                -->
    <!--                                           -->
    <script type="text/javascript" language="javascript"
src="gwtandjavascript/gwtandjavascript.nocache.js"></script>
    <script type="text/javascript" language="javascript"
src="button.js"></script>
  </head>

  <!--                                           -->
  <!-- The body can have arbitrary html, or      -->
  <!-- you can leave the body empty if you want  -->
  <!-- to create a completely dynamic UI.        -->
  <!--                                           -->
  <body>

    <!-- OPTIONAL: include this if you want history support -->
    <iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1'
style="position:absolute;width:0;height:0;border:0"></iframe>

    <!-- RECOMMENDED if your web app will not function without JavaScript
enabled -->
    <noscript>
      <div style="width: 22em; position: absolute; left: 50%; margin-left:
-11em; color: red; background-color: white; border: 1px solid red; padding:
4px; font-family: sans-serif">
        Your web browser must have JavaScript enabled
        in order for this application to display correctly.
      </div>
    </noscript>

    <table align="center">
      <tr>
        <td id="sendButtonContainer"></td>
      </tr>
      <tr>
        <td colspan="2" style="color:red;" id="errorLabelContainer"></td>
      </tr>
    </table>
  </body>
</html>


====================
The javascript code
====================

function createButton(divElement) {
  var button= document.createElement('input');
  button.setAttribute('type','button');
  button.setAttribute('name','button');
  button.setAttribute('value','JS Button');
  divElement.appendChild(button);
  button.onclick = function() { $wnd.sayHello; };

  return buttonnode;
}

// This is called from java
function javascriptSayHello(name) {
  alert("Hello " + name);
}

============


Any help would be highly appreciated :-)

thanks in advance

Boris

-- 
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-web-tool...@googlegroups.com.
To unsubscribe from this group, send email to 
google-web-toolkit+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-web-toolkit?hl=en.

Reply via email to