简体   繁体   English

JavaFX:如何在 TableView 中禁用滚动条

[英]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.我有一个 JavaFX TableView ,当父节点调整得太小而无法显示数据时,我希望表格行为截断TableView的图像,而不是它变得可滚动。 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.下面是我在 SceneBuilder 中的节点层次结构,以防万一。

在此处输入图片说明

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 .我读了这篇文章,但答案只是使单元格调整大小以适应宽度,而不是禁用ScrollBar I read the documentation on ScrollBar s here , but I couldn't find a visible or enabled property.我在这里阅读了关于ScrollBar的文档,但我找不到visibleenabled属性。 I also read the TableView documentation here with no luck.我也在这里阅读了TableView文档,但没有运气。

I searched the JavaFX CSS guide here and found that there are the two policies below that can refer to a scroll pane.我在这里搜索了 JavaFX CSS 指南,发现下面有两个策略可以引用滚动窗格。

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

But wrapping the TableView in a ScrollPane did not work as expected.但是将TableView包装在ScrollPane中没有按预期工作。 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.我想引用这些属性,但如果可能的话,直接在TableView引用。 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:完全隐藏滚动条,无论它们是否“应该”显示,都可以使用以下 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 :如果要禁用所有滚动,则可以向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:如果您不希望TableView在父级太小时缩小,请将最小尺寸设置为使用首选项尺寸:

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.如果您不希望TableView超出其首选大小,请对最大大小执行相同操作。 You can also give an explicit pref size if you want.如果需要,您还可以提供明确的首选项大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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