miniHessel
miniHessel

Reputation: 846

Get selected TableRows original index in TableView

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

Upvotes: 1

Views: 12710

Answers (1)

brian
brian

Reputation: 10989

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

}

Upvotes: 1

Related Questions