How to stop WebEngine after closing stage JavaFX?

2019-02-18 13:11发布

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

2条回答
叼着烟拽天下
2楼-- · 2019-02-18 13:33

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楼-- · 2019-02-18 13:41

Java 9, for me works:

webView.getEngine().load("");
查看更多
登录 后发表回答