私のJavaFXアプリケーションでは、読み込みタスクを実装してプログレスバーと組み合わせたいと思います。JavaFX:進行状況バーと結合するデータ読み込みタスク
私はこのようになりますプレゼンテーションモデルがあります:
public class PresentationModel {
private final ObservableList<Country> countries = FXCollections.observableArrayList();
// Wrap the ObservableList in a FilteredList (initially display all data)
private final FilteredList<Country> filteredCountries = new FilteredList<>(countries, c -> true);
// Wrap the FilteredList in a SortedList (because FilteredList is unmodifiable)
private final SortedList<Country> sortedCountries = new SortedList<>(filteredCountries);
private Task<ObservableList<Country>> task = new LoadTask();
public PresentationModel() {
new Thread(task).start();
}
}
とデータをロードするタスク:
public class LoadTask extends Task<ObservableList<Country>> {
@Override
protected ObservableList<Country> call() throws Exception {
for (int i = 0; i < 1000; i++) {
updateProgress(i, 1000);
Thread.sleep(5);
}
ObservableList<Country> countries = FXCollections.observableArrayList();
countries.addAll(readFromFile());
return countries;
}
}
これは私は、タスクの進捗プロパティにProgressIndicator pi
をバインドすることを可能にします:
pi.progressProperty().bind(model.getTask().progressProperty());
ここで、プレース内のタスクからロードされたデータを取得する必要がありますテーブルに要素を追加することができます:table = new TableView<>(model.getSortedCountries());
ロードタスクからどのようにデータにアクセスできますか?