Questions tagged [jsni]

JSNI is a means to include raw JavaScript code in a GWT application written in Java. JSNI is the web equivalent of inline assembly code.

JSNI (JavaScript Native Interface) can be used to integrate GWT with an existing JavaScript library, or to access low-level browser functionality not exposed by the GWT class API's. JSNI allows you to integrate JavaScript directly into your application's Java source code.

A JSNI method is declared native and contains JavaScript code in a specially formatted comment block between the end of the parameter list and the trailing semicolon. JSNI methods are called just like any normal Java method, and can accept parameters, call methods on Java objects, and return values to the Java-side of the application.

class JavaScriptCalculator 
{
  public static native int add(int n, int m) 
  /*-{ return n + m; }-*/;

  public static void main(String[] args)
  {
    System.out.println(JavaScriptCalculator.add(4, 8));
  }
}

The names $wnd and $doc are used to access the browser's window and document objects from JSNI.

public static native void say(String message) 
/*-{ $wnd.alert(message); }-*/;
365 questions
19
votes
1 answer

Something other than an int was returned from JSNI method

I am running a GWT application in hosted mode. Sporadically I get a strange HostedModeException complaining about the type of the JS value returned from JSNI. Sometimes it is during deserialization: com.google.gwt.dev.shell.HostedModeException:…
Saintali
  • 4,482
  • 2
  • 29
  • 49
19
votes
1 answer

Does GWT JSNI support callbacks?

I am building a GWT app that uses Web SQL Local Storage ( http://dev.w3.org/html5/webdatabase/ ). The problem is that the Web SQL API uses callback functions as arguments. Is it possible to pass "Java" callbacks to JSNI?
Julio Faerman
  • 13,228
  • 9
  • 57
  • 75
17
votes
2 answers

Calling GWT Java function from JavaScript

I am a newcomer to GWT and JavaScript. There are similar question of this type I have tried to follow, but I keep failing. I have a GWT app, I need to call a Java function from Javascript( on the onclick of a href tag, in particular.) Following is…
frodo
  • 1,561
  • 3
  • 21
  • 36
14
votes
1 answer

Calling a GWT Java function from an html script tag

I have a GWT project and I would like to add a script tag to the main html file of the GWT project that calls a Java function located in my client code. According to the documentation I should add something like the following html tag:
Mike
  • 223
  • 2
  • 4
  • 7
12
votes
3 answers

Use jquery inside GWT jsni

I am not able to use $("#"+profileId).offset().top from inside my gwt jsni function. I tried this $wnd.$("#"+profileId).offset().top but this is also not working. I feel i am missing syntax here. Could anybody help
javalearner
  • 3,314
  • 7
  • 26
  • 33
11
votes
1 answer

GWT 2.x $entry function

Cannot find any developer information about this function. I just know that it's suggested to wrap JSNI JavaScript calls to Java methods with this $entry function. I found that it catches exceptions so Java code could handle them. Is it all it does?
nordlig.ulv
  • 125
  • 6
10
votes
1 answer

How to call GWT java function from Javascript?

Is it possible to call Java (GWT) methods from Javascript? It is also unclear from documentation. All samples here http://code.google.com/intl/ru/webtoolkit/doc/latest/DevGuideCodingBasicsJSNI.html demonstrate calling java functions from JSNI (not…
Dims
  • 47,675
  • 117
  • 331
  • 600
9
votes
2 answers

Synchronous RPC Calls in GWT

(That title alone should cause people to come out of the woodwork to bash me with clubs, but hear me out). I have a use case where I need to return a value from a asynchronous call. (I'm using GWT-Platform, but the concepts are the same.) I declared…
Jason
  • 3,943
  • 12
  • 64
  • 104
8
votes
1 answer

How to run JavaScript function from GWT Java with JSNI?

Can't understand from the manual: how actually to run JS function from Java? For example, I have a function in my html page: The…
Dims
  • 47,675
  • 117
  • 331
  • 600
8
votes
1 answer

Scrolling to the top of a page in a gwt application

I've got a gwt application and I want to scroll to the top of a page using this method: public static native void scrollTop() /*-{ $wnd.scroll(0, 0); }-*/; The method is called in the onClick-method of a TreeNodeListenerAdapter: new…
xor_eq
  • 3,933
  • 1
  • 29
  • 33
8
votes
1 answer

GWT - Calling instance method from external javascript

There is this $entry method that we can use in GWT to allow external javascript to execute java methods. You can see the explanations in their documentation…
Anthony Richir
  • 649
  • 2
  • 8
  • 31
7
votes
2 answers

Which is the difference between $doc.getElementById("id") and document.getElementById("id") in JSNI

I'm working in a native function inside a GWT application and I've tried this two methods: document.getElementById("id") returns null but $doc.getElementById() returns a valid element. Which is the difference (conceptually) between this…
Jésica
  • 71
  • 1
  • 2
6
votes
1 answer

How do I wrap a callback using JavaScript Overlay types (GWT)?

In the Display Object class, I have everything wrapped excepted the events. I can't not figure out the pattern and really need an example. In JavaScript, you create a callback for the object like this: displayObject.onPress = function(event) { …
LoneWolf
  • 176
  • 1
  • 9
6
votes
1 answer

How to use Java varargs with the GWT Javascript Native Interface? (aka, "GWT has no printf()")

I'm trying to quickly learn GWT as part of a new project. I found out that GWT doesn't implement Java's String.format() function, so there's no printf()-like functionality. I knew that some printf() implementations exist for Javascript, so I…
markerikson
  • 63,178
  • 10
  • 141
  • 157
6
votes
2 answers

GWT object parameter from javascript to java (JavaScriptObject to JSONObject)

I have Object that I wish to pass from JavaScript to Java in GWT app. This object may have arbitary fields. So it is different from very similar question were only number is passed. Passing javascript parameter from external javascript to java I…
Eugene
  • 257
  • 1
  • 6
1
2 3
24 25