如何知道JavaFx TableView上是否显示滚动条 [英] How to know if a scroll bar is visible on a JavaFx TableView

查看:139
本文介绍了如何知道JavaFx TableView上是否显示滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a way to know if a scroll bar is present on a table view? (Other than what I did in my code below) My goal is to position 2 arrow images (to close/open a side panel) on the right side of a table (over the table.). But I don't want to put them over the scroll bar. The table content is a result of a search, so sometime the scroll bar is visible and other time it is not. If there is not enough items. I want the position of my arrows to change every time the tableview items change.

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

I already try the following solution, but the result is that the arrows are moved the second time I do the search. Looks like a concurrency problem. Like if my listener code is executed before the table is rendered.

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

Is there a way to solve that?

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


推荐答案

我假设你知道这不是一个好主意依靠TableView的内部实现。话虽如此,你的代码看起来非常好(我为无限滚动示例)。

I assume you know that it is not a good idea to rely on the internal implementation of TableView. Having said that, you're code looks mostly good (I did a similar thing for an infinite scrolling example).

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

However you should also consider the case that the scrollbar may appear due to the main window changing its size.

因此我建议你听一下滚动条的visibilty属性的变化。

I would therefore suggest you listen to changes in the visibilty-property of the scrollbar.

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

这篇关于如何知道JavaFx TableView上是否显示滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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