Tableview visible rows


Question

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

1
3
10/2/2014 10:47:24 AM

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.

6
11/12/2014 12:59:51 PM

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