4

When i create new stage with WebEngine that playing video from YouTube, after i close it - Youtube keeps playing on backgroung. If i use "Platform.exit" - its close all my JavaFX App, but i want to close only stage that been created for YouTube.

This is my class for YouTube player:

public class YouTube_player  {
    public YouTube_player(String url) {
        final Group root = new Group();
        Scene scene = new Scene(root, 820, 480);

        final Stage stage = new Stage();
        final WebView webView = new WebView();
        final WebEngine webEngine = webView.getEngine();
        webEngine.loadContent(url);
        root.getChildren().add(webView);
        stage.centerOnScreen();

        stage.setScene(scene);
        stage.show();
        stage.setOnCloseRequest(new EventHandler<WindowEvent>(){

            @Override
            public void handle(WindowEvent event) {
              //What i should put here to close only this stage.
              //Platform.exit - closes all my stages.
              //webEngine.getLoadWorker().cancel(); - dont stop Youtube )))
            }
        });

    }
}

My Youtube player stage is creating after i clicking on button in 'mainstage':

b1.setOnAction(new EventHandler<ActionEvent>() {
    @Override public void handle(ActionEvent event) {
        new YouTube_player(url_video_p1);
    }
});
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
user3425552
  • 43
  • 1
  • 3

2 Answers2

5

You cant dispose the webengine the only thing that you can do is to set the content of the webEngine to null

webView.getEngine().load(null);

ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
  • This does not work if the WebEngine is stuck in a JS loop. Is there a way to stop the ScriptEngine to run? – Frederic Leitenberger Mar 13 '15 at 09:07
  • @Frederic Leitenberger Did you try an event handler or a CountdownLatch to kill the loop. How about Platform.exit()? – Andrew Scott Evans Jan 03 '17 at 23:56
  • `Platform.exit()` does not work since it will shutdown the whole subsystem. So you can't when you still want to use it in other windows / views. A recovery from `Platform.exit()` is not possible without restarting the JVM. – Frederic Leitenberger Jan 11 '17 at 09:07
  • @AndrewScottEvans I don't know how a `CountdownLatch` could stop the JS script (this is VM code). And about "killing the loop" - also don't know how... the only way i know is interrupt or kill the thread, but that might not be possible, too, or cause other bad side effects. – Frederic Leitenberger Jan 11 '17 at 09:09
  • There is one thing though (i noticed a while ago) that relates to this: It seems any JS script is silently stopped after 10 seconds (anyway). But it's not of much use here, too. "Waiting 10 secs" when trying to show a new content doesn't sound reasonable. And the fact that it silently stops scripts is also not reasonable. It should at least log something and/or have a setting to control it. – Frederic Leitenberger Jan 11 '17 at 09:13
  • @Frederic Leitenberger http://www.java67.com/2015/07/how-to-stop-thread-in-java-example.html – Andrew Scott Evans Jan 11 '17 at 17:17
1

Java 9, for me works:

webView.getEngine().load("");
hoefling
  • 59,418
  • 12
  • 147
  • 194
appsofteng
  • 152
  • 5