How detect which column selected in JavaFX TableView?


Question

How to detect, which column(name or id whatever) was selected when user clicked on certain cell?

1
1
6/26/2014 10:21:52 PM

Accepted Answer

To enable individual cells to be selected, instead of entire rows, call

tableView.getSelectionModel().setCellSelectionEnabled(true);

To keep track of which cell(s) are selected, you can do

final ObservableList<TablePosition> selectedCells = table.getSelectionModel().getSelectedCells();
selectedCells.addListener(new ListChangeListener<TablePosition>() {
    @Override
    public void onChanged(Change change) {
        for (TablePosition pos : selectedCells) {
            System.out.println("Cell selected in row "+pos.getRow()+" and column "+pos.getColumn().getTitle());
        }
    });
});
3
7/14/2015 5:54:47 PM

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