ProgressIndicator
非同期バックグラウンドListView
アイテムの読み込み中に を表示しようとしています 。私が望む動作は次のとおりです。
ListView
アイテムの読み込みを開始する前ProgressIndicator
に、不確定な進行状況を表示します。ListView
アイテムの読み込みを非同期に開始します。- アイテムの
ListView
読み込みが完了したら、ProgressIndicator
.
これが私の失敗した試みのssceです:
public class AsyncLoadingExample extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
final ListView<String> listView = new ListView<String>();
final ObservableList<String> listItems = FXCollections.observableArrayList();
final ProgressIndicator loadingIndicator = new ProgressIndicator();
final Button button = new Button("Click me to start loading");
primaryStage.setTitle("Async Loading Example");
listView.setPrefSize(200, 250);
listView.setItems(listItems);
loadingIndicator.setVisible(false);
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// I have hoped it whould start displaying the loading indicator (actually, at the end of this
// method execution (EventHandler.handle(ActionEvent))
loadingIndicator.setVisible(true);
// asynchronously loads the list view items
Platform.runLater(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000l); // just emulates some loading time
// populates the list view with dummy items
while (listItems.size() < 10) listItems.add("Item " + listItems.size());
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
loadingIndicator.setVisible(false); // stop displaying the loading indicator
}
}
});
}
});
VBox root = VBoxBuilder.create()
.children(
StackPaneBuilder.create().children(listView, loadingIndicator).build(),
button
)
.build();
primaryStage.setScene(new Scene(root, 200, 250));
primaryStage.show();
}
}
この例では、ListView
項目は非同期で読み込まれます。ただし、 ProgressIndicator
表示されません。この例でも、すべてのPlatform.runLater(...)
コードを省略するProgressIndicator
と が表示されますが、もちろん、ListView
アイテムは読み込まれません。
したがって、どうすれば目的の動作を実現できますか?