2

In Swing, if I create a JEditorPane like so:

    JEditorPane jep = new JEditorPane();
    jep.setSize(300, Integer.MAX_VALUE);
    jep.setText(content);
    int wrapHeight = jep.getPreferredSize().height;

wrapHeight gives me the height of the content when the width is 300, which I can then use to wrap the component around its content.

I'm trying to do the same thing with a WebView in JavaFX, but when I do this:

  WebView wv = new WebView();
  wv.setMaxWidth(300); 
  wv.getEngine().loadContent(content);
  int wrapHeight =  wv.getPrefHeight();       

(having added the WebView to a Pane), wrapHeight remains 600 (presumably the default).

Following the answer here, I tried inserting the line

 String heightText = WV.getEngine().executeScript(
      "window.getComputedStyle(document.body, null).getPropertyValue('height')"
    ).toString();

but heightText always comes back as 0px.

Having loaded content into a WebView, how can I determine the height of that content at a fixed width?

Update: If I call:

engine.documentProperty().addListener(new ChangeListener<Document>() {
        @Override
        public void changed(ObservableValue<? extends Document> prop, Document oldDoc, Document newDoc) {
            String heightText = WV.getEngine().executeScript(
                    "window.getComputedStyle(document.body, null).getPropertyValue('height')").toString();
            double height = Double.valueOf(heightText.replace("px", ""));

            System.out.println(height);
        }
    });

I get a value returned of 2023.0. However, the height of the content loaded is far less than that - less than 500 px. Where is 2023 coming from?

Community
  • 1
  • 1
drew moore
  • 31,565
  • 17
  • 75
  • 112
  • Open the html content in web browser and run the mentioned js script in browser's console. If it is still 2023.0 then try to understand the js script. – Uluk Biy Feb 01 '14 at 10:42
  • Did you find solution? I also tried this solution http://stackoverflow.com/a/15753155/4568213 and the result is not true. I had 480px but the code showed result 414px. –  Jun 05 '15 at 05:56

0 Answers0