如何基于排序的过滤列表在JavaFX中向TableView添加新行? [英] How to add new Rows to TableView in JavaFX, based on a sorted filtered List?

查看:225
本文介绍了如何基于排序的过滤列表在JavaFX中向TableView添加新行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的FXMLTableViewController.java中,添加了一个搜索功能,该功能使用排序的筛选列表在同一TableView中显示搜索结果(筛选结果).该代码来自这里,这是我在项目中实现的.但是当我执行通常的data2.add(new Entity(""));

In my FXMLTableViewController.java I have added a Search feature which uses the Sorted Filtered List to display the search Results (filter results) in the same TableView. This code was taken from here this is what I have implemented in my Project. But it throws exception when I do the usual data2.add(new Entity(""));

public class FXMLTableViewController {

@FXML
private TableView<Entity> tableView;

@FXML
private JFXTextField searchBox;

@FXML
public void initialize() throws IOException, ParseException {
 tableView.setItems(data2); //data2 is a observable list returned my another function, it is not related here.
    FilteredList<Entity> filteredData = new FilteredList<>   (data2, p -> true);
       searchBox.textProperty().addListener((observable, oldValue,    newValue) -> {
           filteredData.setPredicate(person -> {
              if (newValue == null || newValue.isEmpty()) {
                return true;
            }

            String lowerCaseFilter = newValue.toLowerCase();
            return Person.getPsfName_add().toLowerCase().contains(lowerCaseFilter)
        });
    });
    SortedList<Entity> sortedData = new SortedList<>(filteredData);
    sortedData.comparatorProperty().bind(tableView.comparatorProperty());
    tableView.setItems(sortedData);
    sortedData.add(new Entity("")); //error line 
    tableView.refresh();

}

}

现在,每当我尝试添加新的Entity对象时,都会出现错误

Now whenever I try to add a new Entity object I get the Error

java.lang.UnsupportedOperationException

我想这是由于SortedLists不可修改的事实?我该如何解决?我想在项目的各个位置添加新的行,但是我也想要搜索/过滤器功能.

This I guess is due to the fact that SortedLists are unmodifiable? How do I solve this? I want to add new Rows at various points in my Project but I also want the Search/Filter Facility.

推荐答案

排序(和过滤)的列表不可修改.您应该修改基础列表(data2),排序/过滤后的列表将自动更新(它会观察基础ObservableList).

Sorted (and filtered) lists are unmodifiable. You should modify the underlying list (data2), and the sorted/filtered list will update automatically (it is observing the underlying ObservableList).

您需要的代码如下:

public class FXMLTableViewController {

    @FXML
    private TableView<Entity> tableView;

    @FXML
    private JFXTextField searchBox;

    @FXML
    public void initialize() throws IOException, ParseException {

        FilteredList<Entity> filteredData = new FilteredList<>   (data2, p -> true);
        searchBox.textProperty().addListener((observable, oldValue,    newValue) -> {
            filteredData.setPredicate(person -> {
                if (newValue == null || newValue.isEmpty()) {
                    return true;
                }

                String lowerCaseFilter = newValue.toLowerCase();
                return Person.getPsfName_add().toLowerCase().contains(lowerCaseFilter)
            });
        });
        SortedList<Entity> sortedData = new SortedList<>(filteredData);
        sortedData.comparatorProperty().bind(tableView.comparatorProperty());
        tableView.setItems(sortedData);

        data2.add(new Entity("")); //error line 

    }

}

为清楚起见,请注意,我删除了对tableView.setItems(...)的第一个调用,因为之后您仅将items设置为其他设置.还要注意,tableView.refresh()是多余的(表也在观察组成其项的ObservableList).

Note for clarity I removed the first call to tableView.setItems(...) as you are only setting items to something else immediately afterwards. Also note that tableView.refresh() is redundant (the table is also observing the ObservableList that makes up its items).

这篇关于如何基于排序的过滤列表在JavaFX中向TableView添加新行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆