title: JavaFX WebView loading page in background
body: <p>I have a problem using the JavaFX WebView. What I want to achieve is pre-fetching a web page in the background and visualiszing it only when the page is totally loaded. </p>
  
  <p>I have made a simple exmaple program to reproduce the problem. After the page is loaded I enable a button. A Click on this button then makes the WebView visible.</p>
  
  <p>The problem I have is, that if I click on the button when it gets enabled, the web page is not visible directly. Instead the following happens: At first there is a totally white panel and then after a short time the web page is visible. I don't understand why the page is not visible directly. How can I achieve it, that the web page is directly visible?</p>
  
  <p>The following link points to an animated gif which shows the behaviour:
  <a href="http://tinypic.com/view.php?pic=oh66bl&amp;s=5#.Ujmv1RddWKk" rel="nofollow">http://tinypic.com/view.php?pic=oh66bl&amp;s=5#.Ujmv1RddWKk</a></p>
  
  <pre><code>import java.awt.BorderLayout;
  import java.awt.Dimension;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import javafx.application.Platform;
  import javafx.beans.value.ChangeListener;
  import javafx.beans.value.ObservableValue;
  import javafx.embed.swing.JFXPanel;
  import javafx.scene.Scene;
  import javafx.scene.web.WebEngine;
  import javafx.scene.web.WebView;
  import javax.swing.JFrame;
  import javax.swing.SwingUtilities;
  public class WebViewTest extends javax.swing.JPanel {
  
      private static JFXPanel browserFxPanel;
      private WebView webView;
      private WebEngine eng;
  
      /**
       * Creates new form WebViewTest
       */
      public WebViewTest() {
          initComponents();
          Platform.setImplicitExit(false);
          browserFxPanel = new JFXPanel();
          Platform.runLater(new Runnable() {
              public void run() {
                  webView = createBrowser();
                  Scene scene = new Scene(webView);
                  scene.setFill(null);
                  browserFxPanel.setScene(
                          scene);
              }
          });
      }
  
      /**
       * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The
       * content of this method is always regenerated by the Form Editor.
       */
      @SuppressWarnings("unchecked")
      // &lt;editor-fold defaultstate="collapsed" desc="Generated Code"&gt;                          
      private void initComponents() {
          java.awt.GridBagConstraints gridBagConstraints;
  
          pnlMain = new javax.swing.JPanel();
          showWebpageButton = new javax.swing.JButton();
  
          setLayout(new java.awt.GridBagLayout());
  
          pnlMain.setLayout(new java.awt.BorderLayout());
          gridBagConstraints = new java.awt.GridBagConstraints();
          gridBagConstraints.gridx = 0;
          gridBagConstraints.gridy = 1;
          gridBagConstraints.gridwidth = 3;
          gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
          gridBagConstraints.weightx = 1.0;
          gridBagConstraints.weighty = 1.0;
          add(pnlMain, gridBagConstraints);
  
          showWebpageButton.setText("show web page");
          showWebpageButton.setEnabled(false);
          showWebpageButton.addActionListener(new java.awt.event.ActionListener() {
              public void actionPerformed(java.awt.event.ActionEvent evt) {
                  showWebpageButtonActionPerformed(evt);
              }
          });
          gridBagConstraints = new java.awt.GridBagConstraints();
          gridBagConstraints.gridx = 1;
          gridBagConstraints.gridy = 0;
          gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
          add(showWebpageButton, gridBagConstraints);
      }// &lt;/editor-fold&gt;                        
  
      private void showWebpageButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                  
          pnlMain.removeAll();
          pnlMain.add(browserFxPanel, BorderLayout.CENTER);
          WebViewTest.this.invalidate();
          WebViewTest.this.revalidate();
      }                                                 
      // Variables declaration - do not modify                     
      private javax.swing.JPanel pnlMain;
      private javax.swing.JButton showWebpageButton;
      // End of variables declaration                   
  
      private WebView createBrowser() {
          Double widthDouble = pnlMain.getSize().getWidth();
          Double heightDouble = pnlMain.getSize().getHeight();
          final WebView view = new WebView();
          view.setMinSize(widthDouble, heightDouble);
          view.setPrefSize(widthDouble, heightDouble);
          eng = view.getEngine();
          eng.load("http://todomvc.com/architecture-examples/angularjs/#/");
          eng.getLoadWorker().workDoneProperty().addListener(new ChangeListener&lt;Number&gt;() {
              public void changed(ObservableValue&lt;? extends Number&gt; ov, Number t, Number t1) {
                  final double workDone = eng.getLoadWorker().getWorkDone();
                  final double totalWork = eng.getLoadWorker().getTotalWork();
                  if (workDone == totalWork) {
                      showWebpageButton.setEnabled(true);
                  }
              }
          });
          return view;
      }
  
      public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                  final JFrame f = new JFrame("Navigator Dummy");
                  f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                  f.setSize(new Dimension(1024, 800));
                  final WebViewTest navDummy = new WebViewTest();
                  f.getContentPane().add(navDummy);
                  f.setVisible(true);
              }
          });
      }
  }
  </code></pre>