JavaFX: Disable horizontal scrollbar of TableView

久未见 提交于 2020-01-30 04:47:39

问题


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;
}

Sources: How to hide the horizontal Scrollbar of a ListView in JavaFX and https://community.oracle.com/thread/2377826?tstart=0.

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);

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?

Any help is greatly appreciated!


回答1:


Try to set

table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

without setting pref width of columns.



来源:https://stackoverflow.com/questions/26713162/javafx-disable-horizontal-scrollbar-of-tableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!