【发布时间】:2013-09-09 14:20:23
【问题描述】:
I am trying to make a button that, when multiple rows in a TableView are selected, all of the selected rows are removed.
我正在使用getSelectedIndicies 创建一个可观察列表,但它无法正常工作。
如果我选择前三行,我会在删除它们时打印出索引,然后打印 0,1,然后删除第一行和第三行,但不会删除三行的中间。
delBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
ObservableList<Integer> index =
table.getSelectionModel().getSelectedIndices();
for (int location: index) {
System.out.println(location);
data.remove(location);
}
table.getSelectionModel().clearSelection();
}
});
【问题讨论】:
-
问题是你的for循环。您正在按索引删除。如果您有项目 1,2,3,4 并删除索引 0,那么您现在有 2,3,4。然后删除索引 1(现在是 3)处的项目,留下 2,4。
-
您对我应该怎么做有什么建议吗?我认为这可能与 for 循环有关,我只是不确定如何修复它。