java JavaFX在执行过程时不确定进度条
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30980138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaFX indeterminate progress bar while doing a process
提问by Elyas Hadizadeh
I am trying to insert to my database's one table but this process takes some minutes.
我试图插入到我的数据库的一个表中,但这个过程需要几分钟。
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,但我不知道如何实现它,直到插入到数据库进程结束时才显示它。
回答by clartaq
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.
JavaFX 任务可能是要走的路。它们提供了一种在后台执行长时间运行任务而不会冻结用户界面的方法。
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.
如果您知道您的任务有多少部分,此版本将更新任务完成。如果没有,请注释掉更新进度行中的行,您将得到一个不确定的进度条。
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.
请注意,当长任务正在运行时,您仍然可以增加计数器。另请注意,您可以使用此程序启动多个长时间运行的任务。如果这不是您想要的,您需要稍微调整一下 UI 控件。