layout - 在自定义控件中使用 TextFlow 时,TitledPane 未垂直调整大小

标签 layout javafx controls javafx-8 textflow

我在自定义 JavaFX 控件中使用带有文本的 TextFlow,并且该控件放置在 TitledPane 中。

控制声明:

public class CustomControl extends Control {
    @Override
    protected Skin<?> createDefaultSkin() {
        return new CustomControlSkin(this);
    }
}

皮肤声明:

public class CustomControlSkin extends SkinBase<CustomControl> implements Skin<CustomControl> {
    public CustomControlSkin(CustomControl customControl) {
        super(customControl);
        TextFlow textFlow = new TextFlow();
        textFlow.getChildren().add(new Text("This is a long long long long long long long long long long long long long long text"));
        getChildren().add(new StackPane(textFlow));
    }
}

应用:

@Override
public void start(Stage primaryStage) throws Exception {
    TitledPane titledPane = new TitledPane();
    titledPane.setContent(new CustomControl());
    Scene scene = new Scene(new StackPane(titledPane));
    primaryStage.setScene(scene);
    primaryStage.show();
}

当场景水平调整大小时,文本会换行并且其高度会增加。但是,TitledPane 不会垂直调整大小。

TextFlow used in a custom control

当 TextFlow 直接放置在 TitledPane 中而不使用自定义控件时,不会发生这种情况。

TextFlow used directly in the TitledPane

使用Scenic View我注意到,当在自定义控件中使用 TextFlow 时,控件的布局边界与父级中的边界不同。实际上,parent 中的边界似乎是正确计算的,但没有使用。

Scene graph

这可能是此问题的根源。我已经尝试过皮肤的所有计算(最小/首选/最大)高度方法,但未能正确调整 TitledPane 的大小。

知道为什么 TextFlow 在自定义控件/皮肤中使用时表现不同,以及如何正确调整 TitledPane 的大小吗?

最佳答案

我向 Oracle 报告了此问题,这被视为 JavaFX 错误:JDK-8144128

作为解决此错误的方法,我执行了以下操作:

  • 将控件内容偏差设置为 Orientation.HORIZONTAL

    public class CustomControl extends Control {
        @Override
        protected Skin<?> createDefaultSkin() {
            return new CustomControlSkin(this);
        }
    
        @Override
        public Orientation getContentBias() {
            return Orientation.HORIZONTAL;
        }
    }
    
  • 覆盖皮肤computeMinHeight以在调用node.minHeight时使用节点宽度而不是-1

    public class CustomControlSkin extends SkinBase<CustomControl> implements Skin<CustomControl> {
        public CustomControlSkin(CustomControl customControl) {
            super(customControl);
            TextFlow textFlow = new TextFlow();
            textFlow.getChildren().add(new Text("This is a long long long long long long long long long long long long long long text"));
            getChildren().add(new StackPane(textFlow));
        }
    
        @Override
        protected double computeMinHeight(double width, double topInset, double rightInset, double bottomInset, double leftInset) {
            double minY = 0;
            double maxY = 0;
            boolean firstManagedChild = true;
            for (int i = 0; i < getChildren().size(); i++) {
                Node node = getChildren().get(i);
                if (node.isManaged()) {
                    final double y = node.getLayoutBounds().getMinY() + node.getLayoutY();
                    if (!firstManagedChild) {
                        minY = Math.min(minY, y);
                        maxY = Math.max(maxY, y + node.minHeight(width));
                    } else {
                        minY = y;
                        maxY = y + node.minHeight(width);
                        firstManagedChild = false;
                    }
                }
            }
            double minHeight = maxY - minY;
            return topInset + minHeight + bottomInset;
         }
    }
    

关于layout - 在自定义控件中使用 TextFlow 时,TitledPane 未垂直调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33825689/

相关文章:

Javafx - 场景生成器 - 如何获取表的选定值

c++ - 有什么方法可以将 CString 以外的对象添加到 MFC 中的 CComboBox 中吗?

android - 如何从应用程序范围的主题中排除我的 UI 的某些部分?

Android 布局,左侧有 2 个 TextView,右侧有 CheckBox,复选框上有文本

Android-联系方式UI布局

ios - 从一个应用程序(IOS)控制手机中的其他应用程序

c# - 隐藏ZedGraph控件的灰色边框

css - <div> 以意外的高度呈现,并在为空时破坏布局

css - 鼠标离开元素后,如何在 JavaFX 中重置菜单项悬停颜色?

javafx - 将 JavaFX 中的 ColorPicker 绑定(bind)到 Label Background 属性