title: JavaFX Wait for Page to Load Before Continuing
body: <p>I have a method that sets a new web page for the WebView's WebEngine that needs to wait until the page has finished loading to continue in the current method.</p>
  
  <p>Essentially I want:</p>
  
  <pre><code>MyMethod()
  {
      navigateToNewUrl();
      waitForPageToLoad(); // This is the part I am struggling with
      doSomethingElse();
  }
  </code></pre>
  
  <p>I have tried using ChangeListener() but it will only execute after my method has finished executing. I have googled many terms that lead to more frustration, like "java non-blocking wait for boolean". Eventually I made it as far as starting new threads (to prevent Application GUI from locking up) and using CountDownTimer ( as opposed to Thread.join() ). The following code is my umpteenth attempt at getting to whatI want.</p>
  
  <pre><code>public static Boolean pageLoaded;
  public volatile static CountDownLatch latch;
  
  private static void TestMethod()
  {
      // Set the CountDownLatch
      latch = new CountDownLatch(1);
  
      // Set the page loaded flag
      pageLoaded = false;
  
      // Navigate to new window
      myWebView.getEngine().load("https://www.google.com/");
  
      // Call a method, that starts a thread, that checks if the page has loaded
      WaitForPageLoaded();
  
      // Wait for the CountDownLatch, latch
      try { latch.await(); } catch (InterruptedException e) { /* TODO Auto-generated catch block */ e.printStackTrace(); }
  
      // Write result to console
      System.out.println("[pageLoaded] " + pageLoaded);
  }
  
  private static void WaitForPageLoaded()
  {
      Thread thread = new Thread()
      {
          public void run()
          {
              // 120 iterations * 250 ms = 30 seconds
              for (int i = 0; i &lt; 120; i++)
              {
                  // Check if page has loaded
                  if (pageLoaded == true)
                  {
                      break;
                  }
                  else
                  {
                      System.out.println("Waited " + (i*250) + " milliseconds, so far...");
                      try { Thread.sleep(250); }
                      catch (InterruptedException e) { /* TODO Auto-generated catch block */ e.printStackTrace(); }
                  }
              }
  
              // Tell the calling method to move on with life
              latch.countDown();
          }
      }
  }
  </code></pre>
  
  <p>And in another .java file (class) I have:</p>
  
  <pre><code>        // Get the WebEngine and set a listener for a state change
          WebEngine webEngine = webView.getEngine();
          webEngine.getLoadWorker().stateProperty().addListener
          (
              new ChangeListener&lt;State&gt;()
              {
                  public void changed(@SuppressWarnings("rawtypes") ObservableValue ov, State oldState, State newState)
                  {
                      if (newState == State.SUCCEEDED)
                      {
                          // Set the page loaded flag
                          OtherClassName.pageLoaded = true;
                      }
                  }
              }
          );
  </code></pre>