How to stop WebEngine after closing stage JavaFX?


Question

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);
    }
});
1
3
3/17/2015 10:27:26 PM

Accepted Answer

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);

3
3/16/2014 1:18:52 PM

Java 9, for me works:

webView.getEngine().load("");

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon