Binding a ListView to an Observable Set

I would like to know if there is a way to snap ObservableSetto ListViewor ObservableListbidirectional to ObservableSet?

+4
source share
1 answer

In general, this is not entirely possible, since lists and sets have different functionality (sets are unordered and lists are ordered, lists can contain duplicates, but sets cannot).

You can make sure that it ListViewdisplays items from ObservableSetwith code similar to the following:

ObservableSet<String> set = FXCollections.observableSet();
ListView<String> listView = new ListView<>();
set.addListener((Change<? extends String> c) -> {
    if (c.wasAdded()) {
        listView.getItems().add(c.getElementAdded());
    }
    if (c.wasRemoved()) {
        listView.getItems().remove(c.getElementRemoved());
    }
});

ListView , () listView.getItems() .

+3

Source: https://habr.com/ru/post/1617312/


All Articles