简体   繁体   中英

JavaFX8 JavaScript Callback via WebEngine issue

I am having an issue with the WebEngine class in JavaFX, in which the JavaScript callback just seems to stop working.

webEngine = webView.getEngine();
webEngine.load(getClass().getResource("source.html").toString());
webEngine.setUserStyleSheetLocation(getClass().getResource("style.css").toString());

JSObject js = (JSObject) webEngine.executeScript("window");
js.setMember("app", new JavaLink());

public class JavaLink{
        public void setDrag(boolean drag) {
            System.out.println(drag);
        }
    }

I set the basic code up like this, using oracle docu on WebEngine and created a html file with the following part:

<body>
    <div id = "block">Text</div>

    <script>
        document.getElementById("block").onmouseover = function() {notifyPos()};
        document.getElementById("block").onmouseout = function() {notifyNeg()};

        function notifyPos() {
            app.setDrag(true);
        }

        function notifyNeg() {
            app.setDrag(false);
        }
    </script>
</body>

So whenever I hover over the "Text" it would print true or false. The issue is after some time hovering over and out it just stops working. I have no idea why but adding a background image to the html file via CSS seems to limit it to about ten callbacks (It seems to be time related tho).

EDIT

Okay after testing the code snippet on another PC I noticed that the code doesn't work at all. If someone ever has a similar problem this is what fixed it:

Imports:

import javafx.concurrent.Worker;
import javafx.concurrent.Worker.State;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;

Added code:

webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {    @Override
                public void changed(ObservableValue<? extends Worker.State> ov, Worker.State t, Worker.State t1) {
                    if (t1 == Worker.State.SUCCEEDED) {
                        // this will be run as soon as WebView is initialized.
                        JSObject js = (JSObject) webEngine.executeScript("window");
                        js.setMember("app", new JavaLink());

                    }
                }
            });

I now have the problem that this code works on my Notebook (Same Java version, IDE, addons...) but not on my PC. It still only works for around ten mouseovers. Does anyone know anything what it could be?

Fixed it for my PC since this didn't work apparently or if someone wants to clarify what's happening, the garbage collection deletes the link.

js.setMember("app", new JavaLink());

The code I added to fix was to assign the link beforhand.

JavaLink link = new JavaLink();
js.setMember("app", link);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM