Example usage for javafx.application Platform isFxApplicationThread

List of usage examples for javafx.application Platform isFxApplicationThread

Introduction

In this page you can find the example usage for javafx.application Platform isFxApplicationThread.

Prototype

public static boolean isFxApplicationThread() 

Source Link

Document

Returns true if the calling thread is the JavaFX Application Thread.

Usage

From source file:Main.java

public static boolean isUiThread() {
    return Platform.isFxApplicationThread();
}

From source file:Main.java

public static void executeInFXThread(Runnable runnable) {
    if (Platform.isFxApplicationThread()) {
        runnable.run();//from  w w w  . j  a v  a2 s. co m
    } else {
        Platform.runLater(runnable);
    }
}

From source file:Main.java

public static void runAndWait(Runnable action) {
    if (action == null)
        throw new NullPointerException("action");

    // run synchronously on JavaFX thread
    if (Platform.isFxApplicationThread()) {
        action.run();/*  ww w  .  ja v a2s .c o  m*/
        return;
    }

    // queue on JavaFX thread and wait for completion
    final CountDownLatch doneLatch = new CountDownLatch(1);
    Platform.runLater(() -> {
        try {
            action.run();
        } finally {
            doneLatch.countDown();
        }
    });

    try {
        doneLatch.await();
    } catch (InterruptedException e) {
        // ignore exception
    }
}

From source file:Main.java

/**
 * Creates a popup containing error without any pretty things for a graphical way to show errors
 * @param error Error string to show//from  w  ww . j  ava 2s .c o  m
 */
public static void showError(String error) {
    if (Platform.isFxApplicationThread()) {
        Stage s = new Stage();
        VBox v = new VBox();
        v.getChildren().add(new Label("Error [" + error + ']'));
        Scene sc = new Scene(v);
        s.setTitle("Fail");
        s.setScene(sc);
        s.show();
    } else {
        Platform.runLater(() -> {
            Stage s = new Stage();
            VBox v = new VBox();
            v.getChildren().add(new Label("Error [" + error + ']'));
            Scene sc = new Scene(v);
            s.setTitle("Fail");
            s.setScene(sc);
            s.show();
        });
    }
}

From source file:Main.java

public static void runActionLater(final Runnable runnable) {

    if (Platform.isFxApplicationThread()) {
        runnable.run();//from  w ww.  ja v  a 2  s. c o m
    } else {
        try {
            uiSemaphore.acquire();
            Platform.runLater(() -> {
                try {
                    runnable.run();
                    releaseUiSemaphor();
                } catch (Exception e) {
                    releaseUiSemaphor();
                    throw new RuntimeException(e);
                }
            });
        } catch (Exception e) {
            releaseUiSemaphor();
            throw new RuntimeException(e);
        }
    }

}

From source file:Main.java

/**
 * If called from the FX Application Thread
 * invokes a runnable directly blocking the calling code
 * Otherwise/*from w w w. ja  va  2  s.co  m*/
 * uses Platform.runLater without blocking
 */
static void runOnFxThread(Runnable runnable) {
    if (Platform.isFxApplicationThread()) {
        runnable.run();
    } else {
        Platform.runLater(runnable);
    }
}

From source file:Main.java

static void leaveFXNestedLoop(Object nestedLoopKey) {
    if (!eventLoopKeys.contains(nestedLoopKey))
        return;//w  w  w.  j  a va 2s  .  c  o m

    if (Platform.isFxApplicationThread()) {
        Toolkit.getToolkit().exitNestedEventLoop(nestedLoopKey, null);
    } else {
        Platform.runLater(() -> {
            Toolkit.getToolkit().exitNestedEventLoop(nestedLoopKey, null);
        });
    }

    eventLoopKeys.remove(nestedLoopKey);
}

From source file:Main.java

/**
 * like Platform.runLater but waits until the thread has finished
 * based on: http://www.guigarage.com/2013/01/invokeandwait-for-javafx/
 * @param r the runnable to run in a JavaFX thread
 *//*  w  ww .ja v  a 2 s  . c  o  m*/
public static void platformRunAndWait(final Runnable r) throws Throwable {
    if (Platform.isFxApplicationThread()) {
        try {
            r.run();
        } catch (Exception e) {
            throw new ExecutionException(e);
        }
    } else {
        final Lock lock = new ReentrantLock();
        final Condition condition = lock.newCondition();
        final boolean[] done = { false };
        // to get around the requirement for final
        final Throwable[] ex = { null };
        lock.lock();
        try {

            Platform.runLater(() -> {
                lock.lock();
                try {
                    r.run();
                } catch (Throwable e) {
                    ex[0] = e;
                } finally {
                    try {
                        done[0] = true;
                        condition.signal();
                    } finally {
                        lock.unlock();
                    }
                }
            });

            while (!done[0])
                condition.await();

            if (ex[0] != null) {
                // re-throw exception from the runLater thread
                throw ex[0];
            }
        } finally {
            lock.unlock();
        }
    }
}

From source file:Main.java

static public boolean isFxThread() {
    return Platform.isFxApplicationThread();
}

From source file:de.micromata.mgc.javafx.ControllerService.java

public void runInToolkitThread(final Runnable runnable) {

    if (Platform.isFxApplicationThread() == true) {
        runnable.run();/*from   w  ww  .jav a 2 s. c om*/
        return;
    }

    Platform.runLater(runnable);
}