How to get selected TableCell in JavaFX TableView

javafxjavafx-8

I am looking for a way to get the selected cell of a TableView control. Note that I don't just want the cell value, I want an actual TableCell object. One would expect that the method:

tableView.getSelectionModel().getSelectedCells().get(0)

does just that, but it returns a TablePosition object, which gives you row and column information, but I don't see a way to get TableCell object from that.

The reason I need this is because I want to respond to a key press, but attaching an event filter to TableCell does not work (probably because it is not editable). So I attach it to TableView, but then I need to get the currently selected cell.

EDIT: For future readers: DO NOT mess with TableCell objects, except in cell factory. Use the TableView the way designers intended, or you will be in lot of trouble. If you need data from multiple sources in single table, it is better to make a new class that aggregates all the data and use that as a TableView source.

Best Answer

I just posted an answer that uses this code to edit a Cell. I don't think you can get a reference to the actual table cell as that's internal to the table view.

tp = tv.getFocusModel().getFocusedCell();
tv.edit(tp.getRow(), tp.getTableColumn());

Your method also returns a TablePosition so you can use that as well.

Here's the link https://stackoverflow.com/a/21988562/2855515

Related Topic