Thank you for your suggestions.
At last I found what was wrong (well I found a way to make it work :-) )
First, the call to the method in the onclick was not good. It was defining
an inline function instead of calling directly the variable that was
exported by the MyUtilityClass.
button.onclick = function() { $wnd.sayHello; };
needed to be fixed :
button.onclick = sayHello;
Second the documentation that I have read was probably slightly out of date.
Reading this
http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsJSNI.html#calling(you
can see latest in the url ;-) ) showed a small difference in the
declaration of the new variable sayHello :
$wnd.computeLoanInterest =
$entry(@mypackage.MyUtilityClass::computeLoanInterest(IFI));
instead of
$wnd.computeLoanInterest =
@mypackage.MyUtilityClass::computeLoanInterest(IFI);
I have made those 2 changes and now it is working. I post the code in
case someone has the same problem.
Boris
==============================================
Class test.app.GwtAndJavascript
==============================================
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.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 boutonJs;
>
> public void onModuleLoad() {
> final Button gwtButton = new Button("Calling JS from Java !");
>
> 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) {
> boutonJs.callSayHello("User");
> }
> };
> gwtButton.addClickHandler(handler);
>
> // Now I want to add javascript button that lives in a js script...
> boutonJs = new JsButton();
> panel.add(boutonJs);
>
> }
>
> 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);
> }-*/;
>
> }
>
> }
>
> =============================
Class test.app.MyUtilityClass
=============================
package test.app.client;
import com.google.gwt.user.client.Window;
public class MyUtilityClass {
public static void sayHello() {
Window.alert("Hello from GWT !");
}
public static native void exportStaticMethod() /*-{
$wnd.sayHello = $entry(@test.app.client.MyUtilityClass::sayHello());
}-*/;
}
============================
Javascript button.js
============================
function createButton(divElement) {
var button= document.createElement('input');
button.setAttribute('type','button');
button.setAttribute('name','bouton');
button.setAttribute('value','Calling Java from Javascript');
divElement.appendChild(button);
button.onclick = sayHello;
return button;
}
function javascriptSayHello(name) {
alert("Hello " + name + " from Javascript");
}
=========================
HTML file
=========================
<!doctype html>
<!-- The DOCTYPE declaration above will set the -->
<!-- browser's rendering engine into -->
<!-- "Standards Mode". Replacing this declaration -->
<!-- with a "Quirks Mode" doctype may lead to some -->
<!-- differences in layout. -->
<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="bouton.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>
<h1>Web Application Starter Project</h1>
<table align="center">
<tr>
<td id="sendButtonContainer"></td>
</tr>
<tr>
<td colspan="2" style="color:red;" id="errorLabelContainer"></td>
</tr>
</table>
</body>
</html>
--
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-web-toolkit?hl=en.