在已排序的表格中添加空行

时间:2018-06-20 10:31:06

标签: java javafx

我有一个带有两个按钮(搜索,添加)的表。在这里,搜索按钮会在表格中填充已排序的数据(也已过滤)。现在,我试图在表格底部添加空行。当我的表为空,但有数据时,它显示java.lang.UnsupportedOperationException异常,它工作正常。我猜这是由于SortedLists不可修改的事实。我该如何解决?我想在表格顶部添加新行,但是我也想要搜索/过滤器功能。在这里,我试图获得搜索结果

private ObservableList<ModelBrFloor> getInfo(ModelBrBuilding building) {
            ObservableList list=FXCollections.observableArrayList();
            List<ModelBrFloor> flr= implementation.getFloor(building);
            flr.stream().forEach(list:: add);
                FilteredList <ModelBrFloor> filteredData= new FilteredList<>(list,p->true);
                txtFloorName.textProperty().addListener((observable,oldValue,newValue)->{
                    filteredData.setPredicate(BCode->{
                    if(newValue== null||newValue.isEmpty())
                        return true;
                    String lowerCasefilter=newValue.toLowerCase();
                    if(BCode.getFloorName().toLowerCase().contains(lowerCasefilter))
                        return true;
                    return false;
                    });

                });
                txtFloorShort.textProperty().addListener((observable,oldValue,newValue)->{
                filteredData.setPredicate(BCode->{
                    if(newValue== null || newValue.isEmpty())
                        return true;
                    String lowerCasefilter= newValue.toLowerCase();
                    if(BCode.getFloorCode().toLowerCase().contains(lowerCasefilter))
                        return true;
                    return false;
                });

                });


                SortedList sortedData = new SortedList(filteredData);
                sortedData.comparatorProperty().bind(tableFloor.comparatorProperty());

        return sortedData;

        }

在这里,我试图添加一个空行

@FXML
        private void addFloor() {

            tableFloor.getItems().add(0,new ModelBrFloor());// Error line

        }

NB我见过this,但我不清楚。

1 个答案:

答案 0 :(得分:3)

您不允许修改SortedList / FilteredList,因为它们只是另一个列表的视图。您需要修改原始列表:

private ObservableList<ModelBrFloor> data;

private ObservableList<ModelBrFloor> getInfo(ModelBrBuilding building) {
    data = FXCollections.observableArrayList(implementation.getFloor(building));

    FilteredList<ModelBrFloor> filteredData = new FilteredList<>(list);
    txtFloorName.textProperty().addListener((observable, oldValue, newValue)->{
        if (newValue == null || newValue.isEmpty()) {
            filteredData.setPredicate(null);
        } else {
            final String lowerCasefilter = newValue.toLowerCase();
            filteredData.setPredicate(BCode -> {
                // keep empty and matching rows
                return isEmptyRow(BCode) || BCode.getFloorName().toLowerCase().contains(lowerCasefilter);
            });
        }

    });
    txtFloorShort.textProperty().addListener((observable, oldValue, newValue) -> {
        if (newValue == null || newValue.isEmpty()) {
            filteredData.setPredicate(null);
        } else {
            final String lowerCasefilter = newValue.toLowerCase();
            filteredData.setPredicate(BCode -> {
                return isEmptyRow(BCode) || BCode.getFloorCode().toLowerCase().contains(lowerCasefilter);
            });

    });

    SortedList<ModelBrFloor> sortedData = new SortedList<>(filteredData);
    sortedData.comparatorProperty().bind(Bindings.createObjectBinding(() -> {
            final Comparator<ModelBrFloor> comparator = tableFloor.getComparator();
            return comparator == null ? null : (a, b) -> {
                // sort empty rows to the top, then use comparator
                if (isEmptyRow(a)) {
                    return isEmptyRow(b) ? 0 : -1;
                } else {
                    return isEmptyRow(b) ? 1 : comparator.compare(a, b);
                }
            };
        },
        tableFloor.comparatorProperty()));

    return sortedData;
}

@FXML
private void addFloor() {
    data.add(0, new ModelBrFloor());
}

isEmptyRow是一种检查ModelBrFloor是否为空的方法。