javaFX 异步更新进度条

javaFX 异步更新进度条


javaFX 异步更新进度条_第1张图片

        Task task = new Task() {

            @Override
            protected Integer call() throws Exception {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            protected void running() {
                // TODO Auto-generated method stub
                super.running();
            }

            @Override
            protected void succeeded() {
                // TODO Auto-generated method stub
                super.succeeded();
            }

            @Override
            protected void failed() {
                // TODO Auto-generated method stub
                super.failed();
            }

            @Override
            protected void updateProgress(long workDone, long max) {
                // TODO Auto-generated method stub
                super.updateProgress(workDone, max);
            }

            @Override
            protected void updateMessage(String message) {
                // TODO Auto-generated method stub
                super.updateMessage(message);
            }

            @Override
            protected void updateTitle(String title) {
                // TODO Auto-generated method stub
                super.updateTitle(title);
            }

        };
        progressBar.progressProperty().bind(task.progressProperty());
        new Thread(task).start();

Task只能用一次,想重复使用需要改用Service

package com.ylw.application.sample;

import javafx.application.Application;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * 下载文件进度条同步更新
 * 
 * @author ylw
 *
 */
public class ProgressSample3 extends Application {

    Service service = new Service() {

        @Override
        protected Task createTask() {
            return new Task() {

                @Override
                protected Integer call() throws Exception {
                    int i = 0;
                    while (i++ < 100) {
                        updateProgress(i, 100);
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    return null;
                };
            };
        }

    };

    @Override
    public void start(Stage stage) {
        Group root = new Group();
        Scene scene = new Scene(root, 300, 250);
        stage.setScene(scene);
        stage.setTitle("下载文件进度条同步更新");

        Label label = new Label("下载进度:");
        ProgressBar progressBar = new ProgressBar();
        progressBar.setProgress(0);
        progressBar.setPrefWidth(200);
        HBox hBox = new HBox();
        hBox.setAlignment(Pos.TOP_CENTER);
        hBox.setPrefHeight(60);
        hBox.getChildren().addAll(label, progressBar);

        Button button = new Button("开始下载");
        button.setOnMouseClicked((e) -> {
            progressBar.progressProperty().bind(service.progressProperty());
            service.restart();
        });

        VBox vBox = new VBox();
        vBox.setSpacing(5);
        vBox.setAlignment(Pos.CENTER);
        vBox.getChildren().addAll(hBox, button);

        scene.setRoot(vBox);
        stage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }
}

你可能感兴趣的:(javaFX)