8

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!

Community
  • 1
  • 1
bashoogzaad
  • 4,611
  • 8
  • 40
  • 65

1 Answers1

5

Try to set

table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

without setting pref width of columns.

Uluk Biy
  • 48,655
  • 13
  • 146
  • 153
  • 1
    Thanks Uluk Biy for your answer! I tried it and I can not scroll horizontally anymore. The downside is that I can not control the width of the columns. The columns all have the same width now. Is there a fix for this? – bashoogzaad Nov 03 '14 at 21:32
  • Then try to set pref widths, but imo they will be effective only for initial rendering. These width values will change on column resize as described in the policy's doc. – Uluk Biy Nov 04 '14 at 05:19
  • @bashoogzaad also see [JavaFX 2 Automatic Column Width](http://stackoverflow.com/questions/10152828/javafx-2-automatic-column-width) – Uluk Biy Nov 04 '14 at 05:20
  • 2
    Is there any way to still be able to set the column widths? The pref widths are now overridden by the policy. – bashoogzaad Nov 06 '14 at 09:36
  • 4
    The scrollbar still appears sometimes. It doesn't do anything useful, but it appears, probably because the table width is like 100.1%. – Grumblesaurus Jul 12 '17 at 02:25