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?