【问题标题】:How to know if a scroll bar is visible on a JavaFx TableView如何知道滚动条在 JavaFx TableView 上是否可见
【发布时间】:2014-09-08 17:13:33
【问题描述】:

有没有办法知道表格视图上是否存在滚动条? (除了我在下面的代码中所做的) 我的目标是在桌子的右侧(桌子上方)放置 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 );
}} );

【问题讨论】:

    标签: javafx javafx-2 javafx-8


    【解决方案1】:

    我假设您知道依赖 TableView 的内部实现并不是一个好主意。话虽如此,您的代码看起来大多不错(我为an infinite scrolling example 做了类似的事情)。

    但是你也应该考虑到滚动条可能会因为主窗口改变它的大小而出现的情况。

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

    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(...)
        }
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-25
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 2011-02-24
      相关资源
      最近更新 更多