首页 > 解决方案 > 尝试从 @FXML TableView 中删除时出现 java.lang.UnsupportedOperationException

问题描述

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

初始化方法:

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

标签: javatableview

解决方案


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

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

返回指定列表的不可修改视图。此方法允许模块为用户提供对内部列表的“只读”访问权限。对返回列表的查询操作“通读”到指定列表,并尝试修改返回的列表,无论是直接还是通过其迭代器,都会导致 UnsupportedOperationException。

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


推荐阅读