How to stop WebEngine after closing stage JavaFX?

你说的曾经没有我的故事 提交于 2019-12-09 01:04:03

问题


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:


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




回答2:


Java 9, for me works:

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


来源:https://stackoverflow.com/questions/22436498/how-to-stop-webengine-after-closing-stage-javafx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!