java.lang.UnsupportedOperationException 用于从 javafx tableview 中删除一行
java.lang.UnsupportedOperationException for removing a row from the javafx tableview
我正在尝试从 javafx 的 table 视图中删除选定的记录。
下面是我如何用数据填充 table:
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);
}
这就是我删除记录的方式:
@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();
}
}
但它给出 java.lang.UnsupportedOperationException
错误。我在我的示例项目中做了同样的事情并且一切顺利。那么,我该如何解决这个问题呢?
从基础列表中删除数据,而不是 filtered/sorted 列表:
FileModel selectedItem = fileTable.getSelectionModel().getSelectedItem();
if (selectedItem != null) {
mainApp.getFileData().remove(selectedItem);
}
SortedList和FilteredList继承了不支持remove(index)的AbstractList的remove方法。您必须从源列表中删除该对象 (mainApp.getFileData())
由于选择的索引可能不是源列表中的正确索引(过滤后),有一种方法可以在源列表中获取正确的索引
sortedData.getSourceIndexFor(mainApp.getFileData(), selectedIndex);
所以您应该将代码更改为
@FXML
private void deleteFile() {
int selectedIndex = fileTable.getSelectionModel().getSelectedIndex();
if (selectedIndex >= 0) {
int sourceIndex = sortedData.getSourceIndexFor(mainApp.getFileData(), selectedIndex);
mainApp.getFileData().remove(sourceIndex);
}
}
我已删除此示例中的 else 原因以将其减少到最低限度。
我正在尝试从 javafx 的 table 视图中删除选定的记录。 下面是我如何用数据填充 table:
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);
}
这就是我删除记录的方式:
@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();
}
}
但它给出 java.lang.UnsupportedOperationException
错误。我在我的示例项目中做了同样的事情并且一切顺利。那么,我该如何解决这个问题呢?
从基础列表中删除数据,而不是 filtered/sorted 列表:
FileModel selectedItem = fileTable.getSelectionModel().getSelectedItem();
if (selectedItem != null) {
mainApp.getFileData().remove(selectedItem);
}
SortedList和FilteredList继承了不支持remove(index)的AbstractList的remove方法。您必须从源列表中删除该对象 (mainApp.getFileData()) 由于选择的索引可能不是源列表中的正确索引(过滤后),有一种方法可以在源列表中获取正确的索引
sortedData.getSourceIndexFor(mainApp.getFileData(), selectedIndex);
所以您应该将代码更改为
@FXML
private void deleteFile() {
int selectedIndex = fileTable.getSelectionModel().getSelectedIndex();
if (selectedIndex >= 0) {
int sourceIndex = sortedData.getSourceIndexFor(mainApp.getFileData(), selectedIndex);
mainApp.getFileData().remove(sourceIndex);
}
}
我已删除此示例中的 else 原因以将其减少到最低限度。