I am using javafx tableview with active sorting and insertion of a new row every millisecond...
I want this functionality:
If I have selected a row then it should remain visible (that is should not go up or down from current visible portion of my table) when new rows are inserted.
This may be the long away around it and it's kind of hackish, but it worked for me when I needed to do something similar.
The gist of the answer is that you need to get access to the
VirtualFlow
member of theTableViewSkin
. That's not as simple as is sounds because the skin isn't loaded until the CSS is parsed. I added aListener
to theskinProperty
of theTableView
and was able to get theVirtualFlow
that way.After you have the
VirtualFlow
, you can listen to the table'sObservableList
for changes and check to make sure the selected item is still in the viewport.There will still be a bit of bookkeeping to manage. For instance, if you wanted to place focus back on the table. Also, it's worth noting that the
VirtualFlow
isn't strictly bound by the visible rectangle of the table, so an item may be considered visible even if it is just outside the viewport. You can study the VirtualFlow for more details.Here's a SSCCE.
JavaFXApplication21.java:
Sample.fxml:
SampleController.java:
I also had the same problem .You can use cellEventHandler class to which implemets EventHandler interface to fired when a cell is selcted in table and set last selected row index to a variable. The class is as followed.
Then your table class render of the table in PanelController Class should be as followed.
The "selectedItem" field should be declared in the panelController class as static field and then that "selectedItem " should set as selected in the table when you refresh the table row data.