问题描述
我有以下问题:我想禁用TableView的水平滚动条.它可能不可见,但TableView也可能不会水平滚动.可以在CSS中设置水平滚动条的可见性:
I have the following question: I would like to disable the horizontal scrollbar of my TableView. It may not be visible, but also the TableView may not be scrolled horizontally either. The visibility of the horizontal scrollbar can be set in the CSS:
.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;
}
来源: 和 https://community.oracle.com/thread/2377826?tstart=0 .
这可以正常工作,但是我仍然可以水平滚动TableView(尽管滚动条不可见).我将列宽设置如下:
This works fine, but I can still scroll the TableView horizontally (although the scrollbar is not visible). I have set the column widths as follows:
col1.setPrefWidth(table.getPrefWidth()*0.40);
col2.setPrefWidth(table.getPrefWidth()*0.20);
col3.setPrefWidth(table.getPrefWidth()*0.20);
当TableView不显示任何行时,此方法有效.但是,当我填充它(通过按一个按钮)时,列会稍微拉伸,这使TableView可以水平滚动.
This works when the TableView does not show any rows. But when I populate it (by pressing a button), the columns are slightly stretched and this enables the TableView to be scrolled horizontally.
是否只能隐藏滚动条,还是可以禁用滚动本身?
Is it only possible to hide the scrollbar, or can you also disable the scrolling itself?
任何帮助将不胜感激!
推荐答案
尝试设置
table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
未设置列的首选宽度.
这篇关于JavaFX:禁用TableView的水平滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!