Andrew
Andrew

Reputation: 8758

WebView not showing anything

I have a pretty simple application that lists rows from a database in a tableview. When the user clicks on a row in that list, the application grabs the XML column from that row, and is supposed to display it in a WebView in the same window. Everything other than actually displaying the XML works fine. I've been beating my head on this for a while, but I'm not getting anywhere. Here's the code that the listener calls:

    @FXML
    private void showXML(QueryRow row) {

        String msg = "";
        try {
            msg = mainApp.getMsg(row.getID().get());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        final String fm = msg;

        System.out.println(msg);

        //tb.setText(msg);

        webEngine = webView.getEngine();
//      webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
//        
//          public void changed(ObservableValue ov, State oldState, State newState) {
//
//            if (newState == Worker.State.SUCCEEDED) {
//              System.out.println("inside");
//              webEngine.load(fm);
//              //stage.setTitle(webEngine.getLocation());
//            }
//
//          }
//        });
        System.out.println("Go baby go!");
        webEngine.load(fm);

    }

What am I missing?

Upvotes: 0

Views: 1360

Answers (2)

Abacus
Abacus

Reputation: 19471

Ok, I just tested a little bit. text/html is the correct way, but you need to do some work on your xml data: You have to escape the XML entities (I use commons-lang3 StringEscapeUtils for that) and then wrap all in a preformatted html string:

public class JavaFXTest extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Test to display XML");

        BorderPane content = new BorderPane();
        WebView webView = new WebView();
        WebEngine webEngine = webView.getEngine();

        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<tag1>\n  <tag2>hello</tag2>\n</tag1>";
        String escaped = StringEscapeUtils.escapeHtml4(xml);
        String html = "<html><head></head><body><pre>" + escaped + "</pre></body>";

        webEngine.loadContent(html, "text/html");

        content.setCenter(webView);
        primaryStage.setScene(new Scene(content, 400, 300));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

This produces the following window:

enter image description here

Addition: You might need to do some pretty printing on the xml before escaping; I just used hard coded line breaks and spaces.

Upvotes: 1

varren
varren

Reputation: 14731

If you want to load XML and fm is not link then you should probably use

webEngine.loadContent(fm);

/**
 * Loads the given HTML content directly. This method is useful when you have an HTML
 * String composed in memory, or loaded from some system which cannot be reached via
 * a URL (for example, the HTML text may have come from a database). As with
 * {@link #load(String)}, this method is asynchronous.
 */
public void loadContent(String content) {
    loadContent(content, "text/html");
}

But this will not make xml visible, so if you want your xml to be displayed, you have to put it in some default html page. Something like this: https://gist.github.com/jewelsea/1463485

or simple way:

webEngine.loadContent(
         <textarea readonly style='width:100%; height:100%'>"+ fm +"</textarea>")

Upvotes: 1

Related Questions