如何知道滚动条在 JavaFx TableView 上是否可见

tle*_*que 5 javafx javafx-2 javafx-8

有没有办法知道表格视图上是否存在滚动条?(除了我在下面的代码中所做的)我的目标是在桌子的右侧(桌子上方)放置 2 个箭头图像(以关闭/打开侧面板)。但我不想把它们放在滚动条上。表格内容是搜索的结果,因此有时滚动条可见,有时则不可见。如果没有足够的物品。我希望每次 tableview 项目更改时我的箭头的位置都会更改。

我已经尝试了以下解决方案,但结果是第二次搜索时箭头会移动。看起来像一个并发问题。就像在呈现表之前执行我的侦听器代码一样。

有没有办法解决这个问题?

tableView.getItems().addListener( (ListChangeListener<LogData>) c -> {    
// Check if scroll bar is visible on the table
// And if yes, move the arrow images to not be over the scroll bar
Double lScrollBarWidth = null;
Set<Node> nodes = tableView.lookupAll( ".scroll-bar" );
for ( final Node node : nodes )
{
    if ( node instanceof ScrollBar )
    {
        ScrollBar sb = (ScrollBar) node;
        if ( sb.getOrientation() == Orientation.VERTICAL )
        {
            LOGGER.debug( "Scroll bar visible : {}", sb.isVisible() );
            if ( sb.isVisible() )
            {
                lScrollBarWidth = sb.getWidth();
            }
        }
    }
}

if ( lLogDataList.size() > 0 && lScrollBarWidth != null )
{
    LOGGER.debug( "Must move the arrows images" );
    tableViewController.setArrowsDistanceFromRightTo( lScrollBarWidth );
}
else
{
    tableViewController.setArrowsDistanceFromRightTo( 0d );
}} );
Run Code Online (Sandbox Code Playgroud)

rli*_*rli 4

我假设您知道依赖 TableView 的内部实现不是一个好主意。话虽如此,您的代码看起来大部分都不错(我为无限滚动示例做了类似的事情)。

但是,您还应该考虑由于主窗口改变其大小而可能出现滚动条的情况。

因此,我建议您聆听滚动条可见性属性的变化。

private ScrollBar getVerticalScrollbar() {
    ScrollBar result = null;
    for (Node n : table.lookupAll(".scroll-bar")) {
        if (n instanceof ScrollBar) {
            ScrollBar bar = (ScrollBar) n;
            if (bar.getOrientation().equals(Orientation.VERTICAL)) {
                result = bar;
            }
        }
    }       
    return result;
}
...
bar.visibleProperty().addListener((ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) -> {  
      // tableViewController.setArrowsDistanceFromRightTo(...)
    }
);
Run Code Online (Sandbox Code Playgroud)