JavaFX:加载数据任务以与进度条结合
对于我的JavaFX应用程序,我想实现一个加载任务,将其与进度条结合起来 我有一个演示模型,如下所示:JavaFX:加载数据任务以与进度条结合,java,multithreading,javafx,progress-bar,javafx-8,Java,Multithreading,Javafx,Progress Bar,Javafx 8,对于我的JavaFX应用程序,我想实现一个加载任务,将其与进度条结合起来 我有一个演示模型,如下所示: public class PresentationModel { private final ObservableList<Country> countries = FXCollections.observableArrayList(); // Wrap the ObservableList in a FilteredList (initially display
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();
}
}
现在我需要在表示模型中加载任务中的数据,以便将元素添加到表中:table=newtableview(model.getSortedCountries())代码>
如何从加载任务访问表示模型中的数据?task
在任务成功时调用了onSucceeded
处理程序。value
属性具有call
方法返回的实例
task.setOnSucceeded(event -> {
ObservableList<Country> countries = (ObservableList<Country>)event.getSource().getValue();
// do something
});
task.setOnFailed(event -> {
Throwable e = event.getSource().getException();
if (e instanceof IOException) {
// handle exception here
}
});
task.setOnSucceeded(event -> {
ObservableList<Country> countries = (ObservableList<Country>)event.getSource().getValue();
// do something
});
task.setOnFailed(event -> {
Throwable e = event.getSource().getException();
if (e instanceof IOException) {
// handle exception here
}
});