Tableview可见行 [英] Tableview visible rows

查看:117
本文介绍了Tableview可见行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为tableview实现自定义滚动功能。因此,需要访问可见的行和单元格。

I want to implement a custom scroll functionality for a tableview. Therefore access to the visible rows and cells is needed.

如何检测JavaFX tableview中的可见单元格和行?我没有在tableview的API描述中找到任何相关内容。

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.

谢谢

推荐答案

在TableView中尝试滚动到想要的行。我已经尝试过使用table.scrollTo(index),但它没有正确滚动,因为它将所需行设置为第一个可见行。

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);

在列表中添加元素后,请记得在调用scrollTo之前调用 loadVirtualFlow ,因此VirtualFlow会更新并且不会抛出异常。

After adding elements to the list, remember to call loadVirtualFlow before calling scrollTo, so the VirtualFlow gets updated and doesn't throw an exception.

这篇关于Tableview可见行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆