gpt4 book ai didi

使用多个选择框的 JavaFX 过滤 ListView

转载 作者:行者123 更新时间:2023-12-02 09:52:18 27 4
gpt4 key购买 nike

我当前正在尝试使用通过场景生成器创建的多个选择框来过滤我的 FXML ListView 。

首先,我尝试使用文本字段在我的工作中实现过滤功能,而不是使用选择框,只是为了测试它是否适用于我的自定义 ListView 以及它是否有效。

但现在我无法弄清楚如何使用多个选择框而不是仅使用文本字段来实现它。

希望这里有人能够给我一些关于如何做到这一点的见解,谢谢。

这些是我的选择框

@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());
}

最佳答案

简单地说,您可以将列表的谓词属性与任何 ChoiceBox 的值更改时生成的新谓词值绑定(bind):

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()));

关于使用多个选择框的 JavaFX 过滤 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56235934/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com