首页 > 解决方案 > 带有进度指示器和 runLater 的 JavaFX runAsync

问题描述

我的代码正在异步加载连接:

runAsync {
    controller.loadConnections(viewModel)
} ui {
    table.items = it
    table.refresh()
}

和内部loadConnections方法:

//read connections from file and then 
Platform.runLater(() -> {
    CollectionsKt.bind(connections, databases, ConnectionsModel::new);
    connections.forEach(connection -> connection.setOwner(viewModel));
});

当我想将进度条添加为:

private val status = TaskStatus()
runAsync(status) {
    controller.loadConnections(viewModel)
} ui {
    table.items = it
    table.refresh()
}

但它会阻止进度指示器。据我了解 2 个任务 (progressPlatform.runLater) 没有在async. 但是我怎样才能让它工作呢?

标签: javafxtornadofx

解决方案


试试这个代码:

CompletableFuture.runAsync(() -> {
    for (int n = 1; n > size; n++)
        progressBar.setProgress(n);
});

推荐阅读