Size JavaFX WebView to the minimum size required for the body of the document

I am writing a custom dialog in JavaFX for my project. I need an option, but a quick mode to display the contents of the dialog box.

I know about controlsFX; I actually use it, but I want to show content in a WebView so that it is more manageable.

The problem is that the size of the WebView I want to find the height of the body of the document and set the size of the WebView for it.

I tried the following javascript hack, but before showing the dialog, I get 0 for the size and after showing the dialog the value is not reliable. Is there a better solution, or should I forget about it and try to build a swing component that supports HTML 3?

    WebView vw = new WebView();
    vw.getEngine().loadContent("<p>This is my text</p><b>Another</b><p> This is a paragraph</p><ul><li>First cause</li><li>Second cause></li></ul>");
    vw.setPrefSize(500, 50);
    vw.setOnMouseClicked(new EventHandler<MouseEvent>() {

        @Override
        public void handle(MouseEvent event) {
            // I see that executeScript doesn't work correctly even when the WebView is loaded
            Integer h = (Integer) vw.getEngine().executeScript("document.body.offsetHeight");
            System.out.println("scriptRES: " + h );
            vw.setPrefSize(400, h);
            vw.setMinSize(400, h);
            vw.setMaxSize(400, h);
        }
    });
+1
2

WebView JavaFX:

. .

. WebView engine.executeScript("document.width") engine.executeScript("document.height"). , .

+1

.

ChangeListener , . : Webview, Tabelcell

, - .

-1

Source: https://habr.com/ru/post/1015836/


All Articles