我目前正在尝试使用通过场景构建器创建的多个选择框来过滤我的FXML列表视图。
好吧,我一开始尝试使用文本字段在我的工作中实现过滤器功能,而不是仅仅使用一个选择框来测试它是否与我的自定义列表视图一起工作。
但是现在我很难弄清楚如何使用多个选择框而不是仅使用文本字段来实现它。
希望这里的人能够给我一些有关如何执行此操作的见解,谢谢。
这些是我拥有的选择框
@FXML
private ChoiceBox choiceBox1;
@FXML
private ChoiceBox choiceBox2;
@FXML
private ChoiceBox choiceBox3;
@FXML
private ChoiceBox choiceBox4;
这是我实施过滤器的地方
@Override
public void initialize(URL location, ResourceBundle resources) {
filterField.textProperty().addListener((observable, oldValue, newValue) -> {
filteredData.setPredicate(student -> {
if (newValue == null || newValue.isEmpty()) {
return true;
}
String lowerCaseFilter = newValue.toLowerCase();
if (student.getName().toLowerCase().contains(lowerCaseFilter)) {
return true;
} else if (student.getStatus().toLowerCase().contains(lowerCaseFilter)) {
return true;
}
return false;
});
});
listView.setItems(filteredData);
listView.setCellFactory(studentListView -> new StudentListViewCell());
}
答案 0 :(得分:1)
简单来说,您可以将列表的谓词属性与新的谓词值绑定,当任何ChoiceBox
的值更改时,该新的谓词值将生成:
filteredData.predicateProperty().bind(Bindings.createObjectBinding(() -> (Predicate<Student>) student -> {
// comparisons go here...
String studentName = student.getName().toLowerCase();
return choiceBox1.getValue().contains(studentName) || choiceBox2.getValue() == student.getStatus();
}, choiceBox1.valueProperty(), choiceBox2.valueProperty(), choiceBox3.valueProperty(), choiceBox4.valueProperty()));