Get selected TableRows original index in TableView


Question

Is there any way to get the original index of the selected row in TableView before the TableView was sorted or filtered? If not, can I alternatively make my own TableRow object, get it when selected and have a method getOriginalRowIdex() ?

The code tableView.getSelectionModel().getSelectedIndex()); returns the selected index according to the sorted and filtered data, which makes matching the row indexes to the indexes in a list not possible.

 tableView.setRowFactory(tv -> {
            TableRow<ObservableList> row = new TableRow<>();
            row.setOnMouseClicked(event -> {
                if (event.getClickCount() == 2 && (!row.isEmpty())) {
                   label.setText(rowMessages.get(tableView.getSelectionModel().getSelectedIndex()));

                    System.out.println(tableView.getSelectionModel().getSelectedIndex());

                }
            });
            return row;
        });
1
2
11/24/2014 5:21:48 PM

Accepted Answer

FilteredList and SortedList are just wrappers around a regular ObservableList. Since you have to have the original list, just look up the index of the data in the row using list.indexOf().

public class FilteredTable extends Application {
public static void main(String[] args){launch(args);}
    @Override
    public void start(Stage stage) {
        ObservableList<LineItem> items = FXCollections.observableArrayList();
        for (int i = 0;i<10;i++){items.add(new LineItem(i+"'th", i));}

        TableView tableView = new TableView();
        FilteredList<LineItem> evens = new FilteredList<>(items, p->p.amountProperty().get()%2==0);
        SortedList<LineItem> sorted = new SortedList<>(evens);
        sorted.comparatorProperty().bind(tableView.comparatorProperty());
        tableView.setItems(sorted);

        TableColumn<LineItem,String> descCol = new TableColumn<>("desc");
        descCol.setCellValueFactory(new PropertyValueFactory<>("desc"));

        TableColumn<LineItem, Double> amountCol = new TableColumn<>("amount");
        amountCol.setCellValueFactory(new PropertyValueFactory<>("amount"));

        Label label = new Label("click a row");
        tableView.setRowFactory(tv -> {
            TableRow<ObservableList> row = new TableRow<>();
            row.setOnMouseClicked(event -> {
                if (event.getClickCount() == 2 && (!row.isEmpty())) {
                   label.setText(tableView.getSelectionModel().getSelectedIndex()
                           +" <-tbl row, idx in items-> "
                           +items.indexOf(tableView.getSelectionModel().getSelectedItem()));
                }
            });
            return row;
        });

        tableView.getColumns().addAll(descCol,amountCol);

        stage.setScene(new Scene(new VBox(5,tableView,label),300,300));
        stage.show();
    }


    public class LineItem {

        private final StringProperty desc = new SimpleStringProperty();
        private final IntegerProperty amount = new SimpleIntegerProperty();

        public StringProperty descProperty() {return desc;}
        public IntegerProperty amountProperty() {return amount;}

        public LineItem(String dsc, int amt) {
            desc.set(dsc); amount.set(amt);
        }
    }

}
1
11/26/2014 1:44:49 AM

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