UI 应用程序进行一些计算时冻结
UI freezing while application making some calculations
我有 java fx 应用程序,其中包含许多表格视图。我们怎么知道 javafx tableview 中有数据绑定,它在两个方向上工作。因此,当我的应用程序使用来自 tableviws UI 的数据进行计算时冻结,因为我的应用程序不断更新来自 tableviews(ObservableList) 的数据。我确实尝试过使用 Platform.RunLater,但它对我没有帮助。有什么想法吗?
Platform.runLater
本质上 延迟 你的 运行nable - 但它会再次 运行 在 UI-thread 因此在执行期间阻止每个用户输入。
解决方法很简单,use a worker thread :
Task task = new Task<Void>() {
@Override public Void call() {
static final int max = 1000000;
for (int i=1; i<=max; i++) {
if (isCancelled()) {
break;
}
updateProgress(i, max);
}
return null;
}
};
ProgressBar bar = new ProgressBar();
bar.progressProperty().bind(task.progressProperty());
new Thread(task).start();
虽然建议使用 ExecutorService
class,因为它允许更受控制的行为:http://java-buddy.blogspot.de/2012/06/example-of-using-executorservice.html
我有 java fx 应用程序,其中包含许多表格视图。我们怎么知道 javafx tableview 中有数据绑定,它在两个方向上工作。因此,当我的应用程序使用来自 tableviws UI 的数据进行计算时冻结,因为我的应用程序不断更新来自 tableviews(ObservableList) 的数据。我确实尝试过使用 Platform.RunLater,但它对我没有帮助。有什么想法吗?
Platform.runLater
本质上 延迟 你的 运行nable - 但它会再次 运行 在 UI-thread 因此在执行期间阻止每个用户输入。
解决方法很简单,use a worker thread :
Task task = new Task<Void>() {
@Override public Void call() {
static final int max = 1000000;
for (int i=1; i<=max; i++) {
if (isCancelled()) {
break;
}
updateProgress(i, max);
}
return null;
}
};
ProgressBar bar = new ProgressBar();
bar.progressProperty().bind(task.progressProperty());
new Thread(task).start();
虽然建议使用 ExecutorService
class,因为它允许更受控制的行为:http://java-buddy.blogspot.de/2012/06/example-of-using-executorservice.html