JavaFX (2.2) 将内存中的字符串加载到 WebView

JavaFX (2.2) loading a String from memory into a WebView

我必须使用 JavaFX (2.2) WebView 显示一些值。但价值必须查询表格数据库。该值可能包含对外部网页和内部本地文件的一些引用。如何使用户能够使用 WebView 打开那些网页和本地文件。谢谢。

  WebView wv = new WebView();
    wv.getEngine().setCreatePopupHandler(new Callback<PopupFeatures, WebEngine>() {

        @Override
        public WebEngine call(PopupFeatures p) {
            Stage stage = new Stage(StageStyle.UTILITY);
            WebView wv2 = new WebView();
            stage.setScene(new Scene(wv2));
            stage.show();
            return wv2.getEngine();
        }
    });

    String value = "<a href='http://whosebug.com' target='_blank'>Open a URL</a>" +
                      " <a href=\"file:///C:\Users\Dev\debug.log\">Open a File</a> ";

    wv.getEngine().loadContent(value);

    StackPane root = new StackPane();
    root.getChildren().add(wv);

    Scene scene = new Scene(root, 300, 250);

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show(); 

我已经按照以下方式解决了这个问题。为了使这个启用以下 Whosebug post 非常有帮助。谢谢。

    // Set HostService inherit from Application class 
    ServiceUtil.hostServices = getHostServices();

    // HTML code
    final String htmlCode = "<a href='http://whosebug.com ' target='_blank'>Open a Web URL</a>" +
                            "<a href=\"file:///C:\Users\Dev\debug.log\">Open a Local File</a> ";

    // Create a WebView 
    final WebView webView = new WebView();        

    webView.getEngine().loadContent(htmlCode);

    webView.getEngine().locationProperty().addListener(new ChangeListener<String>() {
         @Override
         public void changed(ObservableValue<? extends String> observable, final String oldValue, final String newValue) {

                      if((newValue != null) && (!newValue.trim().equals(""))) {

                          Platform.runLater(new Runnable() {

                              @Override
                              public void run() {

                                  // Reset content as HTML codes
                                  // NB: This will make stop opening URL in WebView
                                  webView.getEngine().loadContent(htmlCode);

                                  // Direct URL to local browser
                                  ServiceUtil.hostServices.showDocument(newValue);
                              }
                          });
                      }
                  }
     });