I want to implement a custom scroll functionality for a tableview. Therefore access to the visible rows and cells is needed.
How to detect the visible cells and rows in a JavaFX tableview? I don't have found anything about that in the API description for tableview.
Thanks
A Tableview object contains various features such has striped rows, pagination, and autosized and autoaligned columns. The pagination option is recommended when loading a lot of data as the table records are inserted on-demand. Table records are only created when requested to be in a page view.
Table views are data-driven, normally getting their data from a data source object that you provide. The data source object manages your app’s data and is responsible for creating and configuring the table’s cells.
The underlying Treeview object and its methods are exposed in the Tableview.view property. A Tableview object contains various features such has striped rows, pagination, and autosized and autoaligned columns. The pagination option is recommended when loading a lot of data as the table records are inserted on-demand.
For changing height, first set the fixedCellSizeProperty of the table view then use it in binding: Adding 30px is for tableview's header. Show activity on this post. Unfortunately, configuration of the visibleRowCount isn't supported in TableView (you might consider filing a feature request in fx' jira - no need, already done years ago ).
Try this in a TableView to scroll to a wanted row. I've tried with table.scrollTo(index) but it doesn't scroll properly as it sets the wanted row as the first visible.
I've written the next code on my controller.
Here you can also see how to get the visible rows.
/**
* Used to manually scroll the Table
*/
private TableViewSkin<?> tableSkin;
private VirtualFlow<?> virtualFlow;
@Override
public void initialize(URL location, ResourceBundle resources) {
...
...
Platform.runLater( ()-> { loadVirtualFlow(); });
}
/**
* Loads the actual VirtualFlow
*/
private void loadVirtualFlow(){
tableSkin = (TableViewSkin<?>) tableContent.getSkin();
virtualFlow = (VirtualFlow<?>) tableSkin.getChildren().get(1);
}
/**
* Scrolls the table until the given index is visible
* @param index to be shown
*/
private void scrollTo(int index){
int first = virtualFlow.getFirstVisibleCell().getIndex();
int last = virtualFlow.getLastVisibleCell().getIndex();
if (index <= first){
while (index <= first && virtualFlow.adjustPixels(-1) < 0){
first = virtualFlow.getFirstVisibleCell().getIndex();
}
} else {
while (index >= last && virtualFlow.adjustPixels(1) > 0){
last = virtualFlow.getLastVisibleCell().getIndex();
}
}
}
With this code you can easily scroll to a desired index:
scrollTo(index);
After adding elements to the list, remember to call loadVirtualFlow before calling scrollTo, so the VirtualFlow gets updated and doesn't throw an exception.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With