Moonlight
Moonlight

Reputation: 222

fit the content to WebView in JavaFx

I have a problem in getting the size of the content of a web view..here is my code

 String heightText = (String) engine.executeScript("window.getComputedStyle(document.body,null).getPropertyValue('height')");
    double height = Double.valueOf(heightText.replace("px", ""));
    String widthText = web1.getEngine().executeScript(
            "window.getComputedStyle(document.body,null).getPropertyValue('width')"
    ).toString();
    double width = Double.valueOf(widthText.replace("px", ""));
    web1.setPrefHeight(height);
    web1.setPrefWidth(width);
    System.out.println(height + "  " + width);


}

but it refers 0 at first time and in the other times it has wrong result.

actually I had to edit my question. the above code shows the size of the previous content.. why is it like this? please help. thank you

Upvotes: 1

Views: 1077

Answers (2)

jewelsea
jewelsea

Reputation: 159436

Ensure that you wait until the document has loaded before retrieving document properties. Use a change listener on the state property of a load worker as outlined in the WebEngine javadoc.

Disclaimer: I haven't tried to do this for a long time, so the technique may or may not work with current and later JavaFX versions.


If you want to generate a layout pass for the scene graph, use the applyCss/layout functions, otherwise modifying the prefWidth of something, then getting the set width will not probably not match-up.


See also:

Upvotes: 0

Sergey Grinev
Sergey Grinev

Reputation: 34508

Make sure this code is called after Stage.show() or code displaying corresponding window. All height and width getters returns zero before UI initialisation.

Upvotes: 0

Related Questions