java.lang.UnsupportedOperationException 尝试从 @FXML TableView 中删除时

java.lang.UnsupportedOperationException when trying to delete from an @FXML TableView

我不明白为什么这个方法行不通,因为它在三天前确实有效。每当我尝试使用该方法(按下按钮)时,数据库操作工作正常,但每当我尝试从实际的 table 视图中删除时程序都会抛出错误,这样用户就不会再看到该行了。我在初始化方法中添加了一个过滤列表,我担心这可能是问题的原因。这是我的代码:

初始化方法:

    private void initialize()
    {
        ObservableList<BloomClient> clients = FXCollections.observableArrayList();
        firstNames.setCellValueFactory(new PropertyValueFactory<>("FirstName"));
        lastNames.setCellValueFactory(new PropertyValueFactory<>("LastName"));
        phoneNumbers.setCellValueFactory(new PropertyValueFactory<>("PhoneNumber"));
        birthdays.setCellValueFactory(new PropertyValueFactory<>("Birthday"));
        startDates.setCellValueFactory(new PropertyValueFactory<>("StartDate"));
        endDates.setCellValueFactory(new PropertyValueFactory<>("ExpireDate"));
        try {
            clients = dBconnect.getClientList();
        } catch (Exception e) {
            e.printStackTrace();
        }
        FilteredList<BloomClient> filteredList = new FilteredList<BloomClient>(clients,b -> true);
        filteredSearch.textProperty().addListener(((observable, oldValue, newValue) ->
                filteredList.setPredicate(person ->
                {
                    if(newValue == null || newValue.isEmpty())
                        return true;//nothing in text field
                    String lowerCaseFilter = newValue.toLowerCase();
                    if (person.getFirstName().toLowerCase().contains(lowerCaseFilter))
                        return true;//check first name
                    else if (person.getLastName().toLowerCase().contains(lowerCaseFilter))
                        return true;//check last name
                    else
                        return false;
                })
                ));
        SortedList<BloomClient> sortedList = new SortedList<>(filteredList);
        sortedList.comparatorProperty().bind(clientList.comparatorProperty());
        clientList.setItems(sortedList);
    }



public void freezeAccount() throws SQLException, ParseException {
        BloomClient person = clientList.getSelectionModel().getSelectedItem();
        dBconnect.sendToFreeze(person);//this works
        dBconnect.deleteClient(person);//this works
        clientList.getItems().remove(person);//java.lang.UnsupportedOperationException
        clientList.refresh();
    }

好吧,这只是一个猜测。但是 clientList 可能是使用 Collections.unmodifiableList() 创建的 List<> 类型。当您尝试修改其中之一时,会抛出 UnsupportedOperationException

public static List unmodifiableList(List<? extends T> list)

Returns an unmodifiable view of the specified list. This method allows modules to provide users with "read-only" access to internal lists. Query operations on the returned list "read through" to the specified list, and attempts to modify the returned list, whether direct or via its iterator, result in an UnsupportedOperationException.

参见:https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#unmodifiableList-java.util.List-

以某种方式弄明白了,但想要更深入的解释。我最终没有从 clientList(TableView) 中删除,而是直接从 clients(ObservableList) 中删除,并使该变量成为全局变量以供其他方法访问。我不确定最初问题背后的原因。

        BloomClient person = clientList.getSelectionModel().getSelectedItem();
        dBconnect.deleteClient(person);
        clients.remove(person);
        clientList.refresh();
    }