JavaFX: How to disable ScrollBars in TableView

不问归期 提交于 2019-12-24 16:08:31

问题


I hope everyone is doing well.

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

Below is my hierarchy 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 setVisible() or setEnabled() 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.

Thank you so much for your time.


回答1:


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.



来源:https://stackoverflow.com/questions/55093764/javafx-how-to-disable-scrollbars-in-tableview

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