JavaMonkey22 JavaMonkey22 - 4 months ago 6
Java Question

Execute a task after the WebView is fully loaded

Is it possible to have a task automatically run after the

WebEngine
finishes loading? I know I can get a
Worker
from
WebEngine
to indicate loading progress, but it seems like repeatedly polling the
Worker
not an elegant solution (i.e. locking up program until page loads). I know I can do this with a
Task
, so can I get the
Task
for
WebEngine
somehow, and add an execute on complete command?

Answer

Sample from the WebView documentation:

webView.getEngine().getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() {
    @Override
    public void changed(ObservableValue<? extends Worker.State> observable, Worker.State oldValue, Worker.State newValue) {
        if (newValue != Worker.State.SUCCEEDED) {
             return;
        }
        // Your logic here
    }
});
Comments