java.lang.UnsupportedOperationException for removing a row from the javafx tableview

为君一笑 提交于 2019-12-02 04:12:37

问题


I am trying to delete the selected record from the tableview in javafx. Below is how I am populating the table with the data:

public void setMainApp(MainAppClass mainApp){
    this.mainApp = mainApp;

    FilteredList<FileModel> filteredData = new FilteredList<>(mainApp.getFileData(), p -> true);

    // 2. Set the filter Predicate whenever the filter changes.
    filterField.textProperty().addListener((observable, oldValue, newValue) -> {
        filteredData.setPredicate(files -> {
            // If filter text is empty, display all files.
            if (newValue == null || newValue.isEmpty()) {
                return true;
            }

            String lowerCaseFilter = newValue.toLowerCase();
            if (files.getFileSubject().toLowerCase().indexOf(lowerCaseFilter) != -1) {
                return true; // Filter matches Subject.
            }
                else if (files.getFileDate().toLowerCase().indexOf(lowerCaseFilter) != -1) {
                return true; // Filter matches last name.
            }
            return false; // Does not match.
        });
    });

    // 3. Wrap the FilteredList in a SortedList. 
    SortedList<FileModel> sortedData = new SortedList<>(filteredData);

    // 4. Bind the SortedList comparator to the TableView comparator.
    sortedData.comparatorProperty().bind(fileTable.comparatorProperty());

    // 5. Add sorted (and filtered) data to the table.

    fileTable.setItems(sortedData);
}

And thats how I am removing the record:

@FXML
private void deleteFile() {
    int selectedIndex = fileTable.getSelectionModel().getSelectedIndex();
    if (selectedIndex >= 0) {
        fileTable.getItems().remove(selectedIndex);
    } else {
        // Nothing selected.
        Alert alert = new Alert(AlertType.WARNING);
        alert.initOwner(mainApp.getPrimaryStage());
        alert.setTitle("No Selection");
        alert.showAndWait();
    }
} 

But it gives java.lang.UnsupportedOperationException error. I have done the same thing in my sample project and it goes fine. So, how can I resolve this issue?


回答1:


Remove the data from the underlying list, not the filtered/sorted list:

FileModel selectedItem = fileTable.getSelectionModel().getSelectedItem();
if (selectedItem != null) {
    mainApp.getFileData().remove(selectedItem);
}



回答2:


SortedList and FilteredList inherit the remove method from AbstractList which does not support remove(index). You have to remove the object from the source list (mainApp.getFileData()) As the selected index might not be the correct index in the source list (after filtering), there is a method to get the correct index in the source list

sortedData.getSourceIndexFor(mainApp.getFileData(), selectedIndex);

So you should change your code to

@FXML
private void deleteFile() {
  int selectedIndex = fileTable.getSelectionModel().getSelectedIndex();
  if (selectedIndex >= 0) {
    int sourceIndex = sortedData.getSourceIndexFor(mainApp.getFileData(), selectedIndex);
    mainApp.getFileData().remove(sourceIndex);
  }
}

I have removed the else cause in this example to reduce it to the minimum.



来源:https://stackoverflow.com/questions/31225349/java-lang-unsupportedoperationexception-for-removing-a-row-from-the-javafx-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!