Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX: Disable horizontal scrollbar of TableView

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!

like image 824
bashoogzaad Avatar asked Nov 03 '14 11:11

bashoogzaad


1 Answers

Try to set

table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

without setting pref width of columns.

like image 72
Uluk Biy Avatar answered Oct 13 '22 23:10

Uluk Biy