1

I'm using the d3 force directed graph to display some data I get from an API. Before I can display it, it runs through a java class, which does write it into the right json format.
Since the programm runs in a JavaFX WebView I have a bridge class, that does have a getter method I can call from the JavaScript.
In my Main class I create a WebView and assign the bridge to it. I initialize my JSON translator and pass the bridge to it.

@Override
public void start(Stage stage) {
    try {
        new JsonTranslator(individual, depth, bridge);
        Scene scene = createScene();
        [...]           
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    launch(args);
}

private Scene createScene() {
    [...]
    try {
        JSObject jsobj = (JSObject) webEngine.executeScript("window");
        jsobj.setMember("java", bridge);
    } catch (Exception e) {
        e.printStackTrace();
    }
    [...]
}

In my JSONTranslator class I write the json and pass it to the bridge

private void writeFile() {
    try {
        bridge.setJSONObject(obj.toJSONString());
        FileWriter file = new FileWriter(
                "C://path/to/some/file.json"
        file.write(obj.toJSONString());
        file.flush();
        file.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    System.out.println("Object:" + obj);
}

I also write it to a file. The data gets printed as expected. Now in my bridge the data is available throughout the getter / setter. In JSToFXBridge.java:

public String getJSONObject(){
    System.out.println("get request: " + json);
    return json;
}

public void setJSONObject(String string){
    this.json = string;
}

Now I call it from my JavaScript

[...]
var draw = function(json, callback) {
[...]
    callback.call(data);
};
var data = java.getJSONObject();
draw(data);

However it does print get request: -my json data- on the console, the json string is compleatly fine. If I copy & paste it from the console to be like this var data = -my json data- in the code it works. Only to asign it directly from the method won't work. I can't figure out why since I try to load it asynchronously. Based on this tutorial. Do I make a mistake in laoding the string? Or is it even a wrong way to do so?

Lars Kotthoff
  • 107,425
  • 16
  • 204
  • 204
Peter
  • 1,844
  • 2
  • 31
  • 55
  • 1
    Show the JS implementation of `java.getJSONObject()`. I strongly suspect that this question is an instance of http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Tomalak Nov 23 '15 at 10:43
  • `getJSONObject()` is a method in JSToFXBridge.java `java` in this context is only a name in the JavaScript to make callbacks to my java classes. Declared here: `JSObject jsobj = (JSObject) webEngine.executeScript("window"); jsobj.setMember("java", bridge);` – Peter Nov 23 '15 at 11:52
  • You call `java.getJSONObject()` in your JS code. That means `java.getJSONObject()` is a JS function that sits in some library. I just want to know its signature and what it returns. – Tomalak Nov 23 '15 at 12:36
  • Coming from this tutorial: https://blogs.oracle.com/javafx/entry/communicating_between_javascript_and_javafx, it is a Java Method I can access over a bridge class. I have to work it out like this, because it is in a JavFX WebView. In the 3rd code block there is everything I wrote on this method. – Peter Nov 23 '15 at 12:43

1 Answers1

1

Good answer / tutorial to asynchronous JavaScript callbacks can be found here. Solution, which created a new problem [ solved as well ], provided here. In general think of this pattern:

function addOne(thenRunThisFunction) {
  waitAMinuteAsync(function waitedAMinute() {
    thenRunThisFunction()
  })
}
addOne(function thisGetsRunAfterAddOneFinishes(){})

Explains it very well

Community
  • 1
  • 1
Peter
  • 1,844
  • 2
  • 31
  • 55