/ DataFX / invokeAndWait for JavaFX

invokeAndWait for JavaFX

Hendrik on 2013/01/01 - 21:55 in DataFX, General, JavaFX

Swing offers the two methods SwingUtilities.invokeAndWait(…) and SwingUtilities.invokeLater(…) to execute a Runnable object on Swings event dispatching thread. You can read more about this methods here.

As I currently know JavaFX provides only Platform.runLater(…) that is the equivalent of SwingUtilities.invokeLater(…). A “runAndWait” method doesn’t exist at the moment. While developing some DataFX stuff and my first Raspberry Pi demo I needed this feature in JavaFX. So I created a runAndWait method that will hopefully be part of DataFX in some future. Until then you can use this code in your project:


It’s working for all my needs. Please give me some feedback if there are any problems or bug.

10 POST COMMENT

Send Us A Message Here

Your email address will not be published. Required fields are marked *

10 Comments
  • Pingback:Java desktop links of the week, January 7 | Jonathan Giles

  • 2013/01/09

    Hi Hendrik,
    nice solution! You should be aware of condition.wait() and change it to while(!myCondition) condition.wait() otherwise a condition.signalAll() from user code can create strange behavior. I also add some while(!myCondition && Application.isActive()) condition.await() to my code because it can happen the a Platform.exit() will destroy the JavaFX application thread and the calling thread will wait for ever.
    Otherwise… nice work

    Thank you

    Andy

    Reply
  • 2013/03/01

    This will also do the trick (for Runnable and Callable):


    private void runAndWait(Runnable runnable) throws InterruptedException, ExecutionException {
    FutureTask future = new FutureTask(runnable, null);
    Platform.runLater(future);
    future.get();
    }

    private T runAndWait(Callable callable) throws InterruptedException, ExecutionException {
    FutureTask future = new FutureTask(callable);
    Platform.runLater(future);
    return future.get();
    }

    Cheers,
    Tom

    Tom Brus
    Reply
  • 2013/05/16

    Any reason why you didn’t use com.sun.javafx.application.PlatformImpl.runAndWait(Runnable)?

    TIa,

    K.

    K
    Reply
    • 2013/05/17

      Hi,
      cause this is a private API and can change any time.

      Reply
  • 2014/10/31

    Thanks… Very useful.

    Dom
    Reply
    • 2014/10/31

      You should have a look at DataFX 8 because this and other cool APIs are part of the DataFX-core module.

      Reply