如何调整JavaFX的ScrollPane中的内容,以适应目前的规模 [英] How to resize JavaFX ScrollPane content to fit current size

查看:4701
本文介绍了如何调整JavaFX的ScrollPane中的内容,以适应目前的规模的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 BorderPane 的ScrollPane 为中心元素。它会自动调整以填充屏幕。在的ScrollPane 的HBox 没有内容,但一拖和下降的目标。因此,我希望它填补其父的ScrollPane

I have a BorderPane with a ScrollPane as the center element. It resizes automatically to fill the screen. Inside the ScrollPane is a HBox that has no content but is a drag-and-drop target. Therefore I want it to fill its parent ScrollPane.

什么是preferred方式做到这一点?

What is the preferred way to do this?

我至今尝试过被重写 ScrollPane.resize(...)的方法来调整的HBox 但似乎相当复杂和非正统的。

What I have tried so far is overriding the ScrollPane.resize(...) method to resize the HBox but it seems rather complicated and unorthodox.

编辑:要添加一些有用的code到这一点,这是我怎么能解决该问题,但我相信,必须有一些更好的方式来做到这一点:

edit: To add some useful code to this, this is how I can workaround the problem, but I believe there has to be some better way to do this:

@Override
public void resize(double width, double height) {
    super.resize(width, height);

    double scrollBarHeight = 2;
    double scrollBarWidth = 2;

    for (final Node node : lookupAll(".scroll-bar")) {
        if (node instanceof ScrollBar) {
            ScrollBar sb = (ScrollBar) node;
            if (sb.getOrientation() == Orientation.HORIZONTAL) {
                System.out.println("horizontal scrollbar visible = " + sb.isVisible());
                System.out.println("width = " + sb.getWidth());
                System.out.println("height = " + sb.getHeight());
                if(sb.isVisible()){
                    scrollBarHeight = sb.getHeight();
                }
            }
            if (sb.getOrientation() == Orientation.VERTICAL) {
                System.out.println("vertical scrollbar visible = " + sb.isVisible());
                System.out.println("width = " + sb.getWidth());
                System.out.println("height = " + sb.getHeight());
                if(sb.isVisible()){
                    scrollBarWidth = sb.getWidth();
                }
            }
        }
    }

    hBox.setPrefSize(width-scrollBarWidth, height-scrollBarHeight);
}

code部分从这里取:如何访问一个ScrollPane 的滚动条

推荐答案

尝试

ScrollPane scrollPane = new ScrollPane();
scrollPane.setFitToHeight(true);
scrollPane.setFitToWidth(true);

不覆盖调整()方法。

这篇关于如何调整JavaFX的ScrollPane中的内容,以适应目前的规模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆