How to call java method from javascript method that located within jsni method?

699 views
Skip to first unread message

Alex Luya

unread,
Dec 11, 2011, 9:12:43 PM12/11/11
to Google Web Toolkit
Like this:
Class A{
private void javaMethod(int a,int b){}

private native void init()/*-{
function OnMouseMove(e) {
//following code doesn't work

this.@p::javaMethod(Ljava/lang/Integer;Ljava/lang/Integer;)(intVal,_intVal);
}
}
}
----------------------------------------------------------------------------------------------------------
As described above,how to solve this problem?

Frank

unread,
Dec 12, 2011, 2:44:47 AM12/12/11
to Google Web Toolkit
Using an example :

Say you have this code in your normal GWT/Java code, in a class
"FunConfigurator" :

public void onJCrop(int x, int y, int x2, int y2)
{
Window.alert("in gwt: (" + x + "," + y + ")");
}

Now to call this from JSNI do the following (only the fc.@ line is of
any importance) :

private static native void activateJCrop(FunConfigurator fc)
/*-{
$wnd.jQuery(function()
{
$wnd.jQuery('#cropbox').Jcrop({
onSelect: showCoords
});
});

function showCoords(c)
{
// variables can be accessed here as
// c.x, c.y, c.x2, c.y2, c.w, c.h
var s = "hallo"

fc.@be.fks.elka.clientgui.client.configurator.fun.FunConfigurator::onJCrop(IIII)
(c.x, c.y, c.x2, c.y2);
};
}-*/;


In detail :
fc.@be.fks.elka.clientgui.client.configurator.fun.FunConfigurator::onJCrop(IIII)
(c.x, c.y, c.x2, c.y2);
fc = The class that contains the java method you want to call. This is
passed as a parameter to the JSNI method
@be.fks.elka.clientgui.client.configurator.fun.FunConfigurator = The
fully qualified classname (packagename + funconfigurator)
onJCrop : The name of the Java method to call
(IIII) : The params are 4 integers (see below for other available
types)
((c.x, c.y, c.x2, c.y2) : The actual parameters to pas from JS to Java


Note that the IIII means : 4 integers.
These are the possible types :
Z boolean
B byte
C char
S short
I int
J long
F float
D double
L fully-qualified-class ; fully-qualified-class
[ type type[]
( arg-types ) ret-type method type


Hope this helps...

Thomas Broyer

unread,
Dec 12, 2011, 4:51:21 AM12/12/11
to google-we...@googlegroups.com
First, 'int' is not java.lang.Integer, so your method signature in JSNI is wrong; it should read javaMethod(II).
(I suppose the "@p::" while javaMethod is defined in class A is over-simplification in your message, but is OK in your code)

You'll also probably a problem with "this", that might not be what you think it is. A common pattern is to assign the current object ("this" at the time) to a variable that you'll reference from your closure:

var that = this;
...
function OnMouseMove(e) {
   that.@p.A::javaMethod(II)(intVal, intVal);
}
Reply all
Reply to author
Forward
0 new messages