2014-11-03 177 views
4

我有以下問題:我想禁用我的TableView的水平滾動條。它可能不可見,但也可能無法水平滾動TableView。水平滾動條的可見性可以在CSS中設置:JavaFX:禁用TableView的水平滾動條

.table-view *.scroll-bar:horizontal *.increment-button, 
.table-view *.scroll-bar:horizontal *.decrement-button { 
    -fx-background-color: null; 
    -fx-background-radius: 0; 
    -fx-background-insets: 0; 
    -fx-padding: 0; 
} 

.table-view *.scroll-bar:horizontal *.increment-arrow, 
.table-view *.scroll-bar:horizontal *.decrement-arrow { 
    -fx-background-color: null; 
    -fx-background-radius: 0; 
    -fx-background-insets: 0; 
    -fx-padding: 0; 
    -fx-shape: null; 
} 

來源:How to hide the horizontal Scrollbar of a ListView in JavaFXhttps://community.oracle.com/thread/2377826?tstart=0

這工作正常,但我仍然可以水平滾動TableView(雖然滾動條不可見)。我已將列寬設置如下:

col1.setPrefWidth(table.getPrefWidth()*0.40); 
col2.setPrefWidth(table.getPrefWidth()*0.20); 
col3.setPrefWidth(table.getPrefWidth()*0.20); 

這適用於TableView不顯示任何行時。但是,當我填充它(通過按下按鈕),列稍微拉伸,這使得TableView水平滾動。

是隻能隱藏滾動條,或者你也可以禁用滾動本身?

任何幫助,非常感謝!

+0

嘗試給列更窄的寬度擺脫舒展。否則提供一個MVCE。 – 2014-11-06 17:30:05

回答

3

嘗試設置

table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY); 

不設置列PREF寬度。

+0

感謝Uluk比伊你的答案!我試過了,我不能水平滾動了。缺點是我無法控制列的寬度。列現在都有相同的寬度。有沒有解決這個問題? – bashoogzaad 2014-11-03 21:32:17

+0

然後嘗試設置pref寬度,但是它們只對初始渲染有效。這些寬度值將根據策略的文檔中所述的列大小調整而改變。 – 2014-11-04 05:19:06

+0

@bashoogzaad也見[JavaFX 2自動列寬](http://stackoverflow.com/questions/10152828/javafx-2-automatic-column-width) – 2014-11-04 05:20:33