2014-11-03 163 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