2023-02-17 383
说我有许多列的TableView,我想添加一个搜索字段以滤除适合某些条件的行,以名称为例搜索.谢谢
说您的TableView myTable填充了myObject Object s.
创建一个Textfield,在这种情况下,我将其命名为FelterField,因此这是一个简单的实现.
FilteredList<myObject> filteredData = new FilteredList<>(data, p -> true);
// 2. Set the filter Predicate whenever the filter changes.
filterField.textProperty().addListener((observable, oldValue, newValue) -> {
filteredData.setPredicate(myObject -> {
// If filter text is empty, display all persons.
if (newValue == null || newValue.isEmpty()) {
return true;
}
// Compare first name and last name field in your object with filter.
String lowerCaseFilter = newValue.toLowerCase();
if (String.valueOf(myObject.getFirstName()).toLowerCase().contains(lowerCaseFilter)) {
return true;
// Filter matches first name.
} else if (String.valueOf(myObject.getLastName()).toLowerCase().contains(lowerCaseFilter)) {
return true; // Filter matches last name.
}
return false; // Does not match.
});
});
// 3. Wrap the FilteredList in a SortedList.
SortedList<myObject> sortedData = new SortedList<>(filteredData);
// 4. Bind the SortedList comparator to the TableView comparator.
sortedData.comparatorProperty().bind(myTable.comparatorProperty());
// 5. Add sorted (and filtered) data to the table.
myTable.setItems(sortedData);
以上所述是小编给大家介绍的创建搜索文本字段以在javafx tableview中搜索,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!
原文链接:https://77isp.com/post/34158.html
=========================================
https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。
数据库技术 2022-03-28
网站技术 2022-11-26
网站技术 2023-01-07
网站技术 2022-11-17
Windows相关 2022-02-23
网站技术 2023-01-14
Windows相关 2022-02-16
Windows相关 2022-02-16
Linux相关 2022-02-27
数据库技术 2022-02-20
抠敌 2023年10月23日
嚼餐 2023年10月23日
男忌 2023年10月22日
瓮仆 2023年10月22日
簿偌 2023年10月22日
扫码二维码
获取最新动态