我有一个进度条,它在我的应用程序中第一次正常运行。进度条根据observableArrayList的大小运行。当我更改observableArrayList的大小并再次运行它或从其他页面返回页面时,它无法正常工作。我只能获得页面中的文本和不确定的进度条。不显示进程计数和已用时间。我把这个逻辑放在了一个Task中。当我将日志行放在特定的地方时,我发现有时它不会在任务调用方法中输入像Platform.runlater(new Runnable)这样的某些地方。请在下面找到代码。我正在为UI部分使用scenebuilder。
public class ProgressBarController implements Initializable {
public static final Stage stage ;
@FXML
ProgressBar progressBar;
@FXML
public static Button cancelButton;
@FXML
Button save;
@FXML
Label totalprocess;
@FXML
Label runningprocess;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
final Task task;
task = new Task<Void>() {
@Override
protected Void call() throws Exception {
for (int i = 0; i < rightObservableArrayList.size(); i++) {
final int k = i + 1;
//setting the value for total number of processes
totalprocess.setText("total number of process");
//setting the value for the current process. It gets updated as the loop increments.
Platform.runLater(new Runnable() {
@Override
public void run() {
runningprocess.setText(Integer.toString(k));
}
});
/*
some processing for the current val
*/
//update the progressBar
updateProgress(i + 1, <totalprocesssize>);
updateMessage(String.valueOf(i));
Platform.runLater(new Runnable() {
@Override
public void run() {
/*
some code to display the total time to run the process
*/
}
});
}
return null;
}
};
progressBar.progressProperty().bind(task.progressProperty());
Label labelCount = new Label();
labelCount.textProperty().bind(task.messageProperty());
new Thread(task).start();
task.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent t) {
///some code
}
}
});
}
@FXML
public void cancelFired(ActionEvent event) throws IOException {
cancelFlag = true;
progress.setScene(cancelButton.getScene());
progress.close();
}
关于为什么会发生这种情况的任何想法都会非常有用。请分享您的想法。