I have a case where I need to filter a ObservableList<Item>
based on some properties of the items (i.e. the condition is internal and not external). I found out that javafx has FilteredList
so I tried it. I could set the predicate and filtering works, until the property value that determines the filtering changes. Setting the predicate is done now like following:
list.setPredicate(t -> !t.filteredProperty().get())
Since the predicate returns boolean and not BooleanProperty, the changes to that property are not reflected on the list.
Is there any easy solution to this? I could try to do some workarounds, e.g. create a separate list and sync that, or reset the predicate every time the property changes in one item hopefully retriggering the filtering, but I first wanted to ask in case someone knows a pretty solution as those workarounds certainly are not.
Create the underlying list with an extractor. This will enable the underlying list to fire update events when the
filteredProperty()
of any elements change. TheFilteredList
will observe these events and so will update accordingly:Quick demo:
If you are using database loading function plus need to filter multiple fields, this solution will help.