我试图从javafx的tableview中删除所选记录。
以下是我如何用数据填充表的方法:
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
错误。我在示例项目中做了同样的事情,一切顺利。那么,如何解决此问题?
参考方案
从基础列表而不是过滤/排序列表中删除数据:
FileModel selectedItem = fileTable.getSelectionModel().getSelectedItem();
if (selectedItem != null) {
mainApp.getFileData().remove(selectedItem);
}
java.lang.ArrayIndexOutOfBoundsException:使用并行流将元素添加到List时 - java我正在努力优化csv文件的某些处理,因此试图加速某些Jackson的实现。所以我有:List<T> testResults=new ArrayList(); Stream<T> testStream= Streams.stream(TestIterator); testStream.parallel().forEach(p->t…
Java:线程主java.lang.NoClassDefFoundError中的异常 - java我正在尝试使Red5 Flash Media Server在我的计算机上工作。我已经安装了它,但是在运行服务器时出现此错误:- Exception in thread "main" java.lang.NoClassDefFoundError: org/red5/server/Bootstrap Caused by: java.lang.…
不兼容的类型:java.lang.Object无法转换为T - java这是我的代码:package datastructures; import java.util.Iterator; public class Stack<T>{ private class Node<T>{ T data; Node next; } private int size; private Node head; privat…
无法解析类型java.lang.CharSequence。从所需的.class文件间接引用它 - java尝试从GitHub编译某些项目时,遇到以下错误The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class files 如果我没有记错的话,这是来自基本JDK的课程。怎么会无法到达? 参考方案 如上面@ hajo-the…
Java:线程池如何将线程映射到可运行对象 - java试图绕过Java并发问题,并且很难理解线程池,线程以及它们正在执行的可运行“任务”之间的关系。如果我创建一个有10个线程的线程池,那么我是否必须将相同的任务传递给池中的每个线程,或者池化的线程实际上只是与任务无关的“工人无人机”可用于执行任何任务?无论哪种方式,Executor / ExecutorService如何将正确的任务分配给正确的线程? 参考方案 …