简体   繁体   中英

JavaFX: How to disable ScrollBars in TableView

I have a JavaFX TableView and I would like the table behavior to truncate the image of the TableView when the parent node resizes too small to display the data, instead of it becoming scrollable. To clarify, I just want the scroll bars disabled or not visible, so they don't show up.

Below is my node hierarchy in SceneBuilder in case that helps.

在此处输入图片说明

Things I've tried below

I read this post , but the answer just makes the cells resize to fit the width, instead of disabling the ScrollBar . I read the documentation on ScrollBar s here , but I couldn't find a visible or enabled property. I also read the TableView documentation here with no luck.

I searched the JavaFX CSS guide here and found that there are the two policies below that can refer to a scroll pane.

-fx-hbar-policy:
-fx-vbar-policy:

But wrapping the TableView in a ScrollPane did not work as expected. It did not allow me to "fit-to-parent". I would like to refer to these properties, but in a TableView directly if that's possible. Any suggestions are greatly appreciated.

Hiding the scroll bars completely, whether they're "supposed" to be displayed or not, can be achieved with the following CSS:

.table-view .scroll-bar * {
    -fx-min-width: 0;
    -fx-pref-width: 0;
    -fx-max-width: 0;

    -fx-min-height: 0;
    -fx-pref-height: 0;
    -fx-max-height: 0;
}

If you want to disable all scrolling then you can add an event filter to the TableView :

table.addEventFilter(ScrollEvent.ANY, Event::consume);

// or if you only want to disable horizontal scrolling
table.addEventFilter(ScrollEvent.ANY, event -> {
    if (event.getDeltaX() != 0) {
        event.consume();
    }
});

If you don't want the TableView to shrink when the parent gets too small, set the min size to use the pref size:

table.setMinSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);

If you don't want the TableView to grow beyond its pref size, do the same with the max size. You can also give an explicit pref size if you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM