JavaFX: Disable horizontal scrollbar of TableView


Question

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
6
5/23/2017 11:47:28 AM

Try to set

table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

without setting pref width of columns.

4
11/3/2014 4:53:12 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon