I am trying to make a button that, when multiple rows in a TableView are selected, all of the selected rows are removed.
I am creating an observable list using getSelectedIndicies
and it isn't working right.
If I select the first three rows, I have it print out the indicies as it removes them and it prints 0,1 and then it removes the first and third row, but the middle of the three rows is not removed.
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 some reason, this works:
b.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
List items = new ArrayList (treeTable.getSelectionModel().getSelectedItems());
data.removeAll(items);
table.getSelectionModel().clearSelection();
}
});
I doubt that the internal implementation of the selectedItems list ( com.sun.javafx.collections.ObservableListWrapper ) might have some bug.
Edit Yes it's definitely a bug: https://javafx-jira.kenai.com/browse/RT-24367
Removing using index can't work since at each suppression the remaining indexes change.
You could remove the selectedItems
:
delBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
data.removeAll(table.getSelectionModel().getSelectedItems());
table.getSelectionModel().clearSelection();
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With