Jugal112 / cs451_checkers

Checkers game made in Java
0 stars 0 forks source link

Java to Javascript communication #17

Open dlaikhter opened 8 years ago

dlaikhter commented 8 years ago

There are two ways of communicating between the js and java side.

JavaOp (Javscript to Java): JavaOp is a class in java that can be called on the js side, the class is defined in JavaOp.java It is instantiated in Browser.java here: JSObject window = (JSObject) webEngine.executeScript("window"); window.setMember("javaOp", new JavaOps()); Here's an use-case example in script.js: function exit(){ javaOp.exit(); } If the method returns something it will be cast to int or string, so if you want to send complicated data structures you will have to make it a json.

When using these methods, make sure your javaOp method isn't blocking, as this will freeze the interface.

webEngine.execute (Java to javascript): This just executes javascript, to test to make sure that it works you can use firebug and write to the console. e.g. Main.browser.webEngine.executeScript("console.log('hello world!')"); If you are making the call from a thread, use Platform.run(), e.g. (example taken from HostWaitingThread.java):

Platform.runLater(new Runnable() {
            @Override
            public void run() {
                Main.browser.webEngine.executeScript("host_continue()");
            }
        });