Example usage for javafx.concurrent Task Task

List of usage examples for javafx.concurrent Task Task

Introduction

In this page you can find the example usage for javafx.concurrent Task Task.

Prototype

public Task() 

Source Link

Document

Creates a new Task.

Usage

From source file:Main.java

public static void runTaskLater(Runnable runnable) {
    Task task = new Task() {
        @Override//from  w  ww  .  j  av  a2s.  c o  m
        protected Object call() throws Exception {
            runnable.run();
            return null;
        }
    };

    new Thread(task).start();

}

From source file:Main.java

@Override
public void start(Stage stage) {
    Group root = new Group();
    Scene scene = new Scene(root, 300, 150);
    stage.setScene(scene);/* ww  w  .  j  a  va 2 s. co  m*/
    stage.setTitle("Sample");

    Task<Void> task = new Task<Void>() {
        @Override
        protected Void call() throws Exception {
            return null;
        }
    };

    task.run();
    System.out.println(task.getMessage());

    stage.show();
}

From source file:Main.java

@Override
public void start(Stage stage) {
    Group root = new Group();
    Scene scene = new Scene(root, 300, 150);
    stage.setScene(scene);//from w w w .j  av  a2 s . c  o m
    stage.setTitle("Sample");

    Task<Integer> task = new Task<Integer>() {
        @Override
        protected Integer call() throws Exception {
            int iterations;
            for (iterations = 0; iterations < 100000; iterations++) {
                if (isCancelled()) {
                    break;
                }
                System.out.println("Iteration " + iterations);
            }
            return iterations;
        }
    };

    task.run();

    //  root.getChildren().add(t);

    stage.show();
}

From source file:Main.java

@Override
public void start(Stage stage) {
    Group root = new Group();
    Scene scene = new Scene(root, 300, 150);
    stage.setScene(scene);/*  w  ww. j  a v a2  s  .c  om*/
    stage.setTitle("Sample");

    Task<Integer> task = new Task<Integer>() {
        @Override
        protected Integer call() throws Exception {
            int iterations;
            for (iterations = 0; iterations < 10000000; iterations++) {
                if (isCancelled()) {
                    updateMessage("Cancelled");
                    break;
                }
                updateMessage("Iteration " + iterations);
                updateProgress(iterations, 10000000);
            }
            return iterations;
        }
    };

    task.run();
    System.out.println(task.getMessage());
    //  root.getChildren().add(t);

    stage.show();
}

From source file:Main.java

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

    Task<Integer> task = new Task<Integer>() {
        @Override
        protected Integer call() throws Exception {
            int iterations;
            for (iterations = 0; iterations < 10; iterations++) {
                if (isCancelled()) {
                    updateMessage("Cancelled");
                    break;
                }
                updateMessage("Iteration " + iterations);
                updateProgress(iterations, 1000);

                try {
                    Thread.sleep(100);
                } catch (InterruptedException interrupted) {
                    if (isCancelled()) {
                        updateMessage("Cancelled");
                        break;
                    }
                }
            }
            return iterations;
        }
    };

    task.run();
    System.out.println(task.getMessage());
    //  root.getChildren().add(t);

    stage.show();
}

From source file:Main.java

@Override
public void start(Stage stage) {
    Group root = new Group();
    Scene scene = new Scene(root, 300, 150);
    stage.setScene(scene);//from  ww w.  ja v a2 s  .c  o  m
    stage.setTitle("Sample");

    final int totalIterations = 90;
    Task<Integer> task = new Task<Integer>() {
        @Override
        protected Integer call() throws Exception {
            int iterations;
            for (iterations = 0; iterations < totalIterations; iterations++) {
                if (isCancelled()) {
                    updateMessage("Cancelled");
                    break;
                }
                updateMessage("Iteration " + iterations);
                updateProgress(iterations, totalIterations);
            }
            return iterations;
        }
    };

    task.run();
    System.out.println(task.getMessage());
    //  root.getChildren().add(t);

    stage.show();
}

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);/* w  w w . j  ava2s  . co 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:Main.java

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

    Task<ObservableList<String>> task = new Task<ObservableList<String>>() {
        @Override
        protected ObservableList<String> call() throws Exception {
            updateMessage("message");
            ObservableList<String> results = FXCollections.observableArrayList();
            for (int i = 0; i < 10; i++) {
                if (isCancelled())
                    break;
                results.add("");
                updateProgress(i, 100);
            }
            return results;
        }
    };
    task.run();
    System.out.println(task.getMessage());

    stage.show();
}

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 w  w  w  .  j a v a2 s. c  o m
    stage.setTitle("Sample");
    Task<Integer> task = new Task<Integer>() {
        @Override
        protected Integer call() throws Exception {
            int iterations = 0;
            for (iterations = 0; iterations < 10; iterations++) {
                if (isCancelled()) {
                    break;
                }
                System.out.println("Iteration " + iterations);
            }
            return iterations;
        }

        @Override
        protected void succeeded() {
            super.succeeded();
            updateMessage("Done!");
        }

        @Override
        protected void cancelled() {
            super.cancelled();
            updateMessage("Cancelled!");
        }

        @Override
        protected void failed() {
            super.failed();
            updateMessage("Failed!");
        }
    };
    task.run();
    System.out.println(task.getMessage());

    stage.show();
}

From source file:Main.java

private Task createWorker(final int numFiles) {
    return new Task() {
        @Override/*from w  ww .j a v  a2 s .  c o  m*/
        protected Object call() throws Exception {
            for (int i = 0; i < numFiles; i++) {
                long elapsedTime = System.currentTimeMillis();

                Thread.sleep(1 * 1000);

                elapsedTime = System.currentTimeMillis() - elapsedTime;
                String status = elapsedTime + " milliseconds";

                updateMessage(status);
                updateProgress(i + 1, numFiles);
            }
            return true;
        }
    };
}