title: Size JavaFX WebView to the minimum size needed by the document body
body: <p>I'm writing a custom dialog in JavaFX for my project. I need a variant but quick mode for showing the dialog content.</p>
  
  <p>I know about controlsFX; actually I'm using it but I want to show the content in a WebView so it will be more controllable.</p>
  
  <p>The problem is about the size of the WebView I want to find the height of the document body and set the size of the WebView to that.</p>
  
  <p>I tried the following javascript hack but before showing the dialog I get 0 for the size and after showing dialog the value is not reliable. Is there a better solution or I should forget about it and try to embed a swing component that supports HTML 3?</p>
  
  <pre><code>    WebView vw = new WebView();
      vw.getEngine().loadContent("&lt;p&gt;This is my text&lt;/p&gt;&lt;b&gt;Another&lt;/b&gt;&lt;p&gt; This is a paragraph&lt;/p&gt;&lt;ul&gt;&lt;li&gt;First cause&lt;/li&gt;&lt;li&gt;Second cause&gt;&lt;/li&gt;&lt;/ul&gt;");
      vw.setPrefSize(500, 50);
      vw.setOnMouseClicked(new EventHandler&lt;MouseEvent&gt;() {
  
          @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);
          }
      });
  </code></pre>