How to get selected TableCell in JavaFX TableView


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:


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.

2/12/2015 8:01:36 PM

Accepted 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

5/23/2017 10:30:18 AM

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