JavaFX 8 - 向右侧的 TitledPane 添加图形

标签 java layout javafx-8

我想在 TitledPane 的标题中添加一个小图标。因此,我设置了一个空标题并添加了一个包含 LabelImageViewHBox 作为图形。通过这种方式,图标显示在靠近文本末尾的位置。我希望它始终显示在 TitledPane 的右边框旁边。 我怎样才能做到这一点? 我还尝试使用 BorderPane 并将 Label 添加到中心,将 ImageView 添加到右侧,但是 BorderPane 没有获得 TitledPane 的最大尺寸。 所以我尝试将 MaxWidth 设置为 Max-Value,但这没有帮助

有人知道该怎么办吗?

**编辑:** 我创建的“自定义”控件将在 stage.setOnShown 中调用的方法中进行初始化。

public class CustomTitledPane extends TitledPane {
private Image alert;
private Image registered;
private Image deleted;
private ImageView img;

public CustomTitledPane(String titleText, Node node) {
    super(titleText, node);
    setAnimated(true);
    setCollapsible(true);
    img = new ImageView();
    img.setFitHeight(10d);
    img.setPreserveRatio(true);
    img.setSmooth(true);
    setGraphic(img);
    setContentDisplay(ContentDisplay.RIGHT);
    // apply css and force layout of nodes
    applyCss();
    layout();

    // title region
    Node titleRegion = lookup(".title");
    // padding
    Insets padding = ((StackPane) titleRegion).getPadding();
    // image width
    double graphicWidth = img.getLayoutBounds().getWidth();
    // arrow
    double arrowWidth = titleRegion.lookup(".arrow-button")
            .getLayoutBounds().getWidth();
    // text
    double labelWidth = titleRegion.lookup(".text").getLayoutBounds()
            .getWidth();

    double nodesWidth = graphicWidth + padding.getLeft()
            + padding.getRight() + arrowWidth + labelWidth;
    System.out.println("w: " + graphicWidth + " " + arrowWidth + " "
            + labelWidth);
    graphicTextGapProperty().bind(widthProperty().subtract(nodesWidth));
    try {
        alert = new Image(new FileInputStream("img/Alert.png"));
        registered = new Image(new FileInputStream("img/Registered.png"));
        deleted = new Image(new FileInputStream("img/Deleted.png"));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
} }

这是 TitledPane 的 CSS:

    .titled-pane {
    -fx-text-fill: #006FD8;
}

.titled-pane > .title {
    -fx-background-color: transparent;
    -fx-border-color: linear-gradient(to right, white 0%, grey 30%, grey 70%, white 100%) transparent transparent transparent;
}

.titled-pane:expanded > .title {
    -fx-border-color: grey transparent transparent transparent;
    -fx-background-color: linear-gradient(to bottom, #DCE7F5, white);
}

.titled-pane:expanded > *.content {
    -fx-border-width: 0;
}

最佳答案

根据 OP 在他编辑的问题上显示的代码,此代码解决了以下事实:带标题的 Pane 是在显示舞台之前在自定义类上的监听器上创建的。

@Override
public void start(Stage primaryStage) {

    Scene scene = new Scene(new StackPane(), 300, 250);

    primaryStage.setScene(scene);

    primaryStage.setOnShown(e -> {
        CustomTitledPane customTitledPane = new CustomTitledPane("Title", new StackPane(new Label("Graphic to the Right")));
        scene.setRoot(customTitledPane);
        customTitledPane.applyCss();
        customTitledPane.layout();

        // title region
        Node titleRegion=customTitledPane.lookup(".title");
        // padding
        Insets padding=((StackPane)titleRegion).getPadding();
        // image width
        double graphicWidth=customTitledPane.getGraphic().getLayoutBounds().getWidth();
        // arrow
        double arrowWidth=titleRegion.lookup(".arrow-button").getLayoutBounds().getWidth();
        // text
        double labelWidth=titleRegion.lookup(".text").getLayoutBounds().getWidth();

        double nodesWidth = graphicWidth+padding.getLeft()+padding.getRight()+arrowWidth+labelWidth;

        customTitledPane.graphicTextGapProperty().bind(customTitledPane.widthProperty().subtract(nodesWidth));
    });

    primaryStage.show();

}

class CustomTitledPane extends TitledPane {

    public CustomTitledPane(String titleText, Node node) {
        super(titleText, node);
        setAnimated(true);
        setCollapsible(true);
        ImageView img = new ImageView(new Image(getClass().getResource("unlock24.png").toExternalForm()));
        img.setFitHeight(10d);
        img.setPreserveRatio(true);
        img.setSmooth(true);
        setGraphic(img);
        setContentDisplay(ContentDisplay.RIGHT);
    }
}

关于JavaFX 8 - 向右侧的 TitledPane 添加图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27100556/

相关文章:

java - 在 Windows 上克隆 java 应用程序

java - 使用 Play Framework 在页面中显示选项列表

java - 获取与泛型类型实例化相对应的 Class 实例

java - 从一个位置共享属性文件

java - 在 Java 中实现 "Selenium driver pool"的正确方法是什么?

html - 文本旋转模糊

html - 如何避免换行填充?

eclipse - 重置 Eclipse 透视布局

java - 在 JavaFX 中使用 3 个阶段时,关闭第三阶段时焦点将转到第二阶段而不是第一阶段

javafx - 设置条形图列宽大小