【发布时间】:2013-11-22 06:15:09
【问题描述】:
在 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;
}
【问题讨论】:
-
我也研究了进度条示例 (docs.oracle.com/javafx/2/threads/jfxpub-threads.htm),但它是逐步的,我的意思是无尽的不断滚动的进度指示器。
-
滚动是什么意思?是animated striped吗? Showing the Progress of a Background Task 部分与您需要的部分有何不同?
-
无法在 JavaFX 中创建多个 GUI 线程。查看answer to some general JavaFX concurrency questions for an explanation。
-
你好,jewelsea。感谢您对 javafx 的支持。滚动意味着标准 javafx.scene.control ProgressIndicator 类。它不同,因为它没有止境,我不计算步数,我希望它从头到尾顺利滚动,以显示应用程序不会挂断,但可以工作。我阅读了其他答案参考作为对这个问题的裁决的一个很好的结论。但我认为这项任务很常见。也许我缺少任何其他方法来显示 GUI 没有挂起并且正在加载窗格?
标签: multithreading user-interface javafx progress-indicator