有没有办法知道表视图中是否存在滚动条? (除了我在下面的代码中所做的)
我的目标是在桌子的右侧(在桌子上方)放置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 );
}} );
最佳答案 我假设您知道依赖TableView的内部实现并不是一个好主意.话虽如此,你的代码看起来非常好(我为
an infinite scrolling example做了类似的事情).
但是,您还应该考虑由于主窗口更改其大小而导致滚动条出现的情况.
因此,我建议你听一下滚动条的visibilty属性的变化.
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(...)
}
);