Pavel Delgado
Pavel Delgado

Reputation: 99

Event Handler when a Row is deleted from Table View

i need to know how to catch the event that happens when a row is deleted from the TableView and the index of the row. At this moment when a row is deleted from the Table View the TableView.getSelectionModel().clearSelection() method is called. But i want is to select the last index available in the Table View.

Tableview.getSelectionModel().clearAndSelect() is not an option, because sometimes a row is deleted automatically.

Regards

Upvotes: 0

Views: 95

Answers (1)

James_D
James_D

Reputation: 209684

For a table with type, for example, Person:

import javafx.collections.ListChangeListener.Change ;

// ....

TableView<Person> table = ... ;

table.getItems().addListener((Change<? extends Person> c) -> {
    while(c.next()) {
        if (c.wasRemoved()) {
            int numRemoved = c.getRemoved().size();
            int index = c.getFrom();
            System.out.println(numRemoved + " items removed from table at index "+index);
        }
    }
});

The ListChangeListener.Change documentation describes the values returned by c.getFrom(), c.getTo(), c.wasRemoved(), c.getAdded(), etc. under various scenarios.

Upvotes: 2

Related Questions