title: JavaFX wait / notify on Javafx thread or nested event loop
body: <p>Is there a way to do a "wait()" on the javafx thread that pauses the execution of a method until a background task finishes but allowing the javafx thread to continue.</p>
  
  <p>I have found a propietary method to do something like this</p>
  
  <p>Toolkit.getToolkit().enterNestedEventLoop(key);
  and 
  Toolkit.getToolkit().exitNestedEventLoop(key, value);</p>
  
  <p>Imagine that you have an @FXML action method when the user clicks a button and inside that method (running on the FX Thread) I want to do the following:</p>
  
  <p>1 get a value from a javafx observable property
  2 disable an area of the screen and display a progress indicator
  3 call a long running task on a server and stop the excution of the action method here without blocking the UI (or in other words, letting the user navigate to another screen while the background task runs)
  4 continue here on the fx thread reenabling the ui and hiding the progress indicator
  5 update the UI with values returned from the server.</p>
  
  <p>I know one can use a javafx Task to achieve a similar thing, but that would require moving 4 and 5 to the suceeded() method of the javafx task and that is not what i want.</p>
  
  <p>We've got a framework which allows the developer to annotate some of the remote service stubs on a javafx controller with a @Background annotation and replaces those stubs with a jdk dynamic proxy which switches all invocations to any method in the stubs to run on a non javafx thread and continue on 4 when the server call has finished so we don't have to fill up our code with javafx thread synchronization code.</p>
  
  <p>The Toolkit.getToolkit().enterNestedEventLoop(key); and Toolkit.getToolkit().exitNestedEventLoop(key, value); work well in most cases, except in some scenarios like:</p>
  
  <ul>
  <li><p>if the invocation happens on the javafx thread at application startup time (prepopulating a screen with server data on startup) </p></li>
  <li><p>if a @FXML action method has two server calls in the same method</p></li>
  </ul>