JavaFX为TableView的列添加自动筛选排序的过滤器

直接上代码

1,定义的过滤器

@FXML private TextField filterTxt;

2,绑定过滤器的共通方法

/**
 * <pre>
 *     为TableColumn绑定过滤器
 * <pre/>
 * 
 * @param tableView TableView
 * @param observableList 筛选的数据集合
 * @param filterValue 用于获取筛选状态为true或false的接口,此处为了
 *                    提共通方法,定义了接口
 * @param newValue 输入的值
 * @param <T> 表的泛型类型
 */
public static final <T> void bindTableViewFilter(TableView<T> tableView, ObservableList<T> observableList, FilterValue<T> filterValue, String newValue){
    FilteredList<T> filteredData = new FilteredList<>(observableList, p->true);
    filteredData.setPredicate(entity -> filterValue.compare(entity, newValue));
    SortedList<T> sortedData = new SortedList<>(filteredData);
    sortedData.comparatorProperty().bind(tableView.comparatorProperty());
    tableView.setItems(sortedData);
}

3,判断是否是筛选结果的方法

private boolean compareKnowhowId(筛选的数据对象 entity, String newValue){
    return .判断逻辑省略;
}

4,接口类定义

package xx.xx.xx.xx.xx;

public interface FilterValue<T> {
    boolean compare(T t, String newValue);
}

5,进行绑定处理

filterTxt.textProperty().addListener((observable, oldValue, newValue) -> ControlUtil.bindTableViewFilter(tableView,observableList, (entity, currentValue)->compareKnowhowId(entity, currentValue), newValue));

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值