Example usage for javafx.application Platform runLater

List of usage examples for javafx.application Platform runLater

Introduction

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

Prototype

public static void runLater(Runnable runnable) 

Source Link

Document

Run the specified Runnable on the JavaFX Application Thread at some unspecified time in the future.

Usage

From source file:Main.java

static void leaveFXNestedLoop(Object nestedLoopKey) {
    if (!eventLoopKeys.contains(nestedLoopKey))
        return;//from   ww  w. ja v  a 2 s .  com

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

    eventLoopKeys.remove(nestedLoopKey);
}

From source file:Main.java

/**
 * Runs the given <tt>Runnable</tt> on the JavaFX Application Thread. The method blocks until the <tt>Runnable</tt> is executed completely.
 * You should use the {@link io.datafx.core.concurrent.ProcessChain} for concurrent tasks and background tasks
 * instead of using this low level API./*from w w  w.j  a v a  2s.  c  o m*/
 *
 *
 * @param runnable the runnable that will be executed on the JavaFX Application Thread
 * @throws InterruptedException if the JavaFX Application Thread was interrupted while waiting
 * @throws ExecutionException   if the call of the run method of the <tt>Runnable</tt> threw an exception
 */
public static void runAndWait(Runnable runnable) throws InterruptedException, ExecutionException {
    FutureTask<Void> future = new FutureTask<>(runnable, null);
    Platform.runLater(future);
    future.get();
}

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  w w.jav  a2 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

public static <T> void invokeLater(Callable<T> callable, JSObject callback) {
    SwingUtilities.invokeLater(() -> {
        try {// w  w  w.  j  ava2  s .  c o  m
            final T result = callable.call();
            if (callback != null) {
                Platform.runLater(() -> callback.call("call", null, result));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
}

From source file:Main.java

public static void runActionLater(Runnable runnable, boolean force) {
    if (force) {/*from w ww .  j a v a 2s .c o  m*/
        Platform.runLater(runnable);
    } else {
        runActionLater(runnable);
    }
}

From source file:Main.java

/**
 * Runs the given <tt>Callable</tt> on the JavaFX Application Thread. The method blocks until the <tt>Callable</tt> is executed completely. The return value of the call() method of the callable will be returned
 * You should use the {@link io.datafx.core.concurrent.ProcessChain} for concurrent tasks and background tasks
 * instead of using this low level API./*w  w w.j  av a2s .  c  o m*/
 *
 * @param callable the callable that will be executed on the JavaFX Application Thread
 * @param <T>      return type of the callable
 * @return return value of the executed call() method of the <tt>Callable</tt>
 * @throws InterruptedException if the JavaFX Application Thread was interrupted while waiting
 * @throws ExecutionException   if the call of the run method of the <tt>Callable</tt> threw an exception
 */
public static <T> T runCallableAndWait(Callable<T> callable) throws InterruptedException, ExecutionException {
    FutureTask<T> future = new FutureTask<T>(callable);
    Platform.runLater(future);
    return future.get();
}

From source file:Main.java

public static void executeWithProgressDialogInBackground(final Runnable runnable, final StackPane target,
        final String text) {
    Thread th = new Thread() {
        @Override/*from w w w  . jav a2s . c  om*/
        public void run() {
            final Node progressIndicator = createProgressIndicator();
            final Label label = new Label(text);
            try {
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        target.getChildren().add(progressIndicator);
                        label.setWrapText(true);
                        target.getChildren().add(label);
                    }
                });
                runnable.run();
            } finally {
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        target.getChildren().remove(progressIndicator);
                        target.getChildren().remove(label);
                    }
                });
            }
        }
    };
    th.setDaemon(true);
    th.start();
}

From source file:Main.java

@Override
public void init() {
    System.out.println("init(): " + Thread.currentThread().getName());

    Runnable task = () -> System.out.println("Running the task on the " + Thread.currentThread().getName());

    // Submit the task to be run on the JavaFX Aplication Thread
    Platform.runLater(task);
}

From source file:Main.java

@Override
public void start(Stage stage) {
    final Group group = new Group();
    Scene scene = new Scene(group, 300, 150);
    stage.setScene(scene);/*from  www  . ja v a 2s. c  o m*/
    stage.setTitle("Sample");

    Task<Void> task = new Task<Void>() {
        @Override
        protected Void call() throws Exception {
            for (int i = 0; i < 10; i++) {
                if (isCancelled())
                    break;
                final Rectangle r = new Rectangle(10, 10);
                r.setX(10 * i + i);
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        group.getChildren().add(r);
                    }
                });
            }
            return null;
        }
    };
    task.run();
    System.out.println(task.getMessage());

    stage.show();
}

From source file:ipat_fx.IPAT_FX.java

@Override
public void start(Stage stage) throws Exception {
    String contextPath = System.getProperty("user.dir") + "/web/";
    File logFile = new File(contextPath + "/log/log4j-IPAT.log");
    System.setProperty("rootPath", logFile.getAbsolutePath());

    Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
    Scene scene = new Scene(root);
    stage.setScene(scene);/*from   w w w.j a v  a 2 s . co  m*/
    stage.show();
    stage.setOnHiding((WindowEvent event) -> {
        Platform.runLater(() -> {
            File src = new File(contextPath + "/Client Data");
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
            Date date = new Date();
            File dest = new File(contextPath + "/Saves/Ipat_" + dateFormat.format(date));
            if (!dest.exists()) {
                dest.mkdirs();
            }
            try {
                FileUtils.copyDirectory(src, dest);
            } catch (IOException ex) {
                Logger.getLogger(IPAT_FX.class.getName()).log(Level.SEVERE, null, ex);
            }
            System.exit(0);
        });
    });
}