Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javafx.application.Application;
- import javafx.application.Platform;
- import javafx.concurrent.Task;
- import javafx.concurrent.Worker;
- import javafx.geometry.Pos;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.Label;
- import javafx.scene.control.ProgressBar;
- import javafx.scene.effect.GaussianBlur;
- import javafx.scene.layout.StackPane;
- import javafx.scene.layout.VBox;
- import javafx.scene.text.Font;
- import javafx.scene.text.FontWeight;
- import javafx.stage.Stage;
- /*
- @Author : Aamir khan
- */
- public class Main extends Application {
- @Override
- public void start(Stage window) throws Exception {
- StackPane root = new StackPane();
- VBox pane = new VBox();
- Task task = new Task<Integer>() {
- @Override
- public Integer call() throws Exception {
- Label counter = new Label();
- counter.setFont(Font.font("Arial", FontWeight.SEMI_BOLD, 25));
- StackPane cover = new StackPane();
- Platform.runLater(() -> {
- cover.getChildren().add(counter);
- pane.setEffect(new GaussianBlur(2.0f));
- root.getChildren().add(cover);
- counter.textProperty().bind(this.messageProperty());
- });
- int i;
- final int max = 100;
- for (i = 1; i <= max; i++) {
- if (isCancelled()) {
- // updateMessage("Task Canclled");
- System.out.println("Task Canclled");
- break;
- }
- updateMessage("Number = " + i);
- System.out.println("Number = " + i);
- updateProgress(i, max);
- try {
- Thread.sleep(50);
- } catch (InterruptedException e) {
- }
- if (isCancelled()) {
- updateMessage("Task Cancelled");
- }
- }
- return i;
- }
- @Override
- public void succeeded() {
- super.succeeded();
- root.getChildren().remove(1);
- pane.setEffect(null);
- }
- };
- task.stateProperty().addListener(
- (ob, o, n) -> {
- if (n.equals(Worker.State.SCHEDULED)) {
- System.out.println("Schedule");
- }
- if (n.equals(Worker.State.SUCCEEDED)) {
- System.out.println("kahatam");
- }
- }
- );
- Button start = new Button("Start");
- start.setOnAction(e -> {
- Thread t = new Thread(task);
- t.setDaemon(true);
- t.start();
- });
- ProgressBar pb = new ProgressBar();
- pb.progressProperty().bind(task.progressProperty());
- pane.setSpacing(5);
- pane.getChildren().addAll(pb, start);
- pane.setAlignment(Pos.CENTER);
- root.getChildren().add(pane);
- window.setTitle("One time tasking in javafx");
- window.setScene(new Scene(root, 400, 250));
- window.show();
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
Add Comment
Please, Sign In to add comment