JavaFX stop opening URL in WebView - open in browser instead

前端 未结 9 1231
無奈伤痛
無奈伤痛 2020-12-09 05:12

The embedded WebView browser I am using needs special handling for particular URLs, to open them in the native default browser instead of WebView. The actual browsing part w

9条回答
  •  春和景丽
    2020-12-09 05:57

    2017 version - still very hacky but much more concise:

    class AboutDialog extends Dialog {
        private final Controller controller;
        private final String url;
    
        AboutDialog() {
            super();
    
            this.controller = Controller.getInstance();
    
            this.setTitle(controller.getProperty("about_title"));
            this.setHeaderText(null);
    
            this.url = getClass().getResource("/about_dialog.html").toExternalForm();
    
            this.setWebView();
    
            this.getDialogPane().getButtonTypes().add(new ButtonType(controller.getProperty("close"), ButtonBar.ButtonData.CANCEL_CLOSE));
            this.getDialogPane().setPrefWidth(600);
        }
    
        private void setWebView() {
            final WebView webView = new WebView();
            webView.getEngine().load(url);
    
            webView.getEngine().locationProperty().addListener((observable, oldValue, newValue) -> {
                controller.getMainFxApp().getHostServices().showDocument(newValue);
                Platform.runLater(this::setWebView);
            });
    
            this.getDialogPane().setContent(webView);
        }
    }
    

提交回复
热议问题