How to add new Rows to TableView in JavaFX, based

2019-09-14 18:16发布

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

}

}

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

java.lang.UnsupportedOperationException

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.

1条回答
啃猪蹄的小仙女
2楼-- · 2019-09-14 18:44

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).

The code you need looks like:

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 

    }

}

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).

查看更多
登录 后发表回答