JavaFX在执行流程时不确定进度条 [英] JavaFX indeterminate progress bar while doing a process

查看:119
本文介绍了JavaFX在执行流程时不确定进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试插入我的数据库的一个表,但这个过程需要几分钟。

I am trying to insert to my database's one table but this process takes some minutes.

我希望在我的应用程序尝试时有一个不确定的进度条插入。为了有进度条,我想使用JavaFX,但我不知道如何实现它,直到插入数据库进程结束才显示它。

I would like to have a indeterminate progress bar while my application is trying to insert. In order to have progress bar, I want to use JavaFX but I don't know how to implement it that it just has be shown until the end of inserting in to the database process.

推荐答案

JavaFX任务可能就是这样。它们提供了一种在后台执行长时间运行任务的方法,而不会冻结用户界面。

JavaFX Tasks are probably the way to go. They provide a way to execute long-running tasks in the background without freezing the user interface.

以下是您可能尝试的示例:

Here's an example of something you might try:

package Demo;

import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class BackgroundWorkerDemo extends Application {

    private final static String CNTR_LBL_STR = "Counter: ";
    private int counter;

    Label counterLabel;
    Button counterButton;
    Button taskButton;

    @Override
    public void start(Stage primaryStage) {
        counter = 0;
        counterLabel = new Label(CNTR_LBL_STR + counter);
        counterButton = new Button("Increment Counter");
        counterButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                counter++;
                counterLabel.setText(CNTR_LBL_STR + counter);
            }
        });
        taskButton = new Button("Long Running Task");
        taskButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
               runTask();
            }
        });

        VBox mainPane = new VBox();
        mainPane.setPadding(new Insets(10));
        mainPane.setSpacing(5.0d);
        mainPane.getChildren().addAll(counterLabel, counterButton, taskButton);
        primaryStage.setScene(new Scene(mainPane));
        primaryStage.show();
    }

    private void runTask() {

        final double wndwWidth = 300.0d;
        Label updateLabel = new Label("Running tasks...");
        updateLabel.setPrefWidth(wndwWidth);
        ProgressBar progress = new ProgressBar();
        progress.setPrefWidth(wndwWidth);

        VBox updatePane = new VBox();
        updatePane.setPadding(new Insets(10));
        updatePane.setSpacing(5.0d);
        updatePane.getChildren().addAll(updateLabel, progress);

        Stage taskUpdateStage = new Stage(StageStyle.UTILITY);
        taskUpdateStage.setScene(new Scene(updatePane));
        taskUpdateStage.show();

        Task longTask = new Task<Void>() {
            @Override
            protected Void call() throws Exception {
                int max = 50;
                for (int i = 1; i <= max; i++) {
                    if (isCancelled()) {
                        break;
                    }
                    updateProgress(i, max);
                    updateMessage("Task part " + String.valueOf(i) + " complete");

                    Thread.sleep(100);
                }
                return null;
            }
        };

        longTask.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
            @Override
            public void handle(WorkerStateEvent t) {
                taskUpdateStage.hide();
            }
        });
        progress.progressProperty().bind(longTask.progressProperty());
        updateLabel.textProperty().bind(longTask.messageProperty());

        taskUpdateStage.show();
        new Thread(longTask).start();
    }

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

}

此版本将更新任务完成如果你知道你的任务将有多少部分。如果没有,请注释掉更新进度的行中的行,您将得到一个不确定的进度条。

This version will update the task completion if you know how many parts your task will have. If not, comment out the line in the line that updates progress and you will get an indeterminate progress bar.

请注意,当长任务正在运行时,您仍然可以增加柜台。另请注意,您可以使用此程序启动多个长时间运行的任务。如果那不是您想要的,您需要稍微调整UI控件。

Note that while the long task is running, you can still increment the counter. Also note that you can start multiple long-running tasks with this program. If that is not what you want, you'll need to fiddle with the UI controls a bit.

这篇关于JavaFX在执行流程时不确定进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆