在 javafx gui 进程是在一个单独的线程上完成的。不允许将进度指示器放在带有 Service 和 Task 的后台线程上,因为它不是 FX 线程并且指示器是 FX 元素。是否可以在 javafx 中创建多个 gui 线程?
或者在加载其他 gui 元素时,是否有另一种方法可以使进度指示器保持滚动?目前它开始滚动,然后卡住,直到加载窗格。
@FXML
public void budgetShow(ActionEvent event) {
progressIndicator = new ProgressIndicator(-1.0);
rootPane.getChildren().add(progressIndicator);
progressIndicator.setVisible(true);
progressIndicator.toFront();
threadBudgetShow().start();
}
public Service<Void> threadBudgetShow() {
Service<Void> service = new Service<Void>() {
@Override
protected Task<Void> createTask() {
return new Task<Void>() {
@Override
protected Void call() throws Exception {
// Background Thread operations.
final CountDownLatch latch = new CountDownLatch(1);
Platform.runLater(new Runnable() {
@Override
public void run() {
try {
// FX Thread opeartions.
// budgetAnchorPane - reload.
if (budgetAnchorPane == null || !budgetAnchorPane.isVisible()) {
budgetAnchorPane = new BudgetAnchorPane();
rootPane.getChildren().add(budgetAnchorPane);
budgetAnchorPane.setVisible(true);
budgetAnchorPane.getChildren().remove(budgetAnchorPane.budgetTypeComboBox);
budgetAnchorPane.budgetTypeComboBox = new BudgetTypeCombobox();
budgetAnchorPane.getChildren().add(budgetAnchorPane.budgetTypeComboBox);
}
} finally {
rootPane.getChildren().remove(progressIndicator);
latch.countDown();
}
}
});
latch.await();
// Other background Thread operations.
return null;
}
};
}
};
return service;
}