2014-07-17 62 views
4

有沒有辦法知道表格視圖中是否存在滾動條? (除了我在下面的代碼中所做的) 我的目標是在桌子的右側(在桌子上)放置2個箭頭圖像(以關閉/打開側面板)。但我不想把它們放在滾動條上。 表格內容是搜索的結果,所以有時滾動條可見,而其他時間則不可見。如果沒有足夠的物品。 我希望我的箭頭位置每次改變tableview項目時都會改變。如何知道JavaFx中是否顯示滾動條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); 
}}); 

回答

2

我假設您知道依靠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(...) 
    } 
); 
相關問題