Issue with removing multiple rows at once from JavaFX TableView


Question

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();
    }
});
1
4
9/9/2013 3:48:26 PM

Accepted Answer

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

6
9/10/2013 4:18:17 PM

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

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon