Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with removing multiple rows at once from JavaFX TableView

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();
    }
});
like image 279
facon12 Avatar asked Jan 13 '23 10:01

facon12


2 Answers

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

like image 76
Rasha Avatar answered Jan 31 '23 19:01

Rasha


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();
    }
});
like image 42
gontard Avatar answered Jan 31 '23 18:01

gontard