Используйте FilteredList
и свяжите его predicate
свойство с Predicate
, которое зависит от входных данных:
private static <T> void replace(ObservableList<T> list, T oldItem, T newItem) {
if (oldItem == null) {
if (newItem != null) {
list.add(newItem);
}
} else {
int index = list.indexOf(oldItem);
if (newItem == null) {
if (index >= 0) {
list.remove(index);
}
} else {
if (index >= 0) {
list.set(index, newItem);
} else {
list.add(newItem);
}
}
}
}
ObservableList<Predicate<SearchField>> predicates = FXCollections.observableArrayList();
property_filter.predicateProperty().bind(Bindings.createObjectBinding(() -> predicates.isEmpty() ? null : new Predicate<SearchField>() {
@Override
public boolean test(SearchField item) {
for (Predicate<SearchField> predicate : predicates) {
if (!predicate.test(item)) {
return false;
}
}
return true;
}
}, predicates));
society_name.textProperty().addListener(new ChangeListener<String>() {
private Predicate<SearchField> predicate;
@Override
public void changed(ObservableValue<? extends String> o, String oldValue, String newValue) {
Predicate<SearchField> oldPredicate = predicate;
predicate = newValue.isEmpty() ? null : i -> i.getprop_society().contains(newValue);
replace(predicates, oldPredicate, predicate);
}
});
comboBox.valueProperty().addListener(new ChangeListener<ItemType>() {
private Predicate<SearchField> predicate;
@Override
public void changed(ObservableValue<? extends ItemType> o, ItemType oldValue, ItemType newValue) {
Predicate<SearchField> oldPredicate = predicate;
predicate = ...;
replace(predicates, oldPredicate, predicate);
}
});
...