JavaFX 11 : Add a graphic to a TitledPane on the right side

标签 java javafx javafx-8 javafx-11

这与三年前提出的一个问题有关。 JavaFX 8 - Add a graphic to a TitledPane on the right side

我不喜欢这个解决方案,我还没有找到任何其他解决方案。

自从针对 JavaFX 8 提出这个原始问题以来,JavaFX 发生了很多变化。最近发布了 JavaFX 11。

我有一个使用 JavaFX 11 的项目。 TitledPane 图形设置有一个 HBox,其中包含一个我想放置在标题最右侧的 Button。

TitledPane titledPane = new TitledPane();
titledPane.setText("Title");

HBox titleBox = new HBox();
Button b1 = new Button("ClickMe!");
titleBox.getChildren().add(b1);

titledPane.setGraphic(titleBox);
titledPane.setContentDisplay(ContentDisplay.RIGHT);

将 TitlePane Alignment 设置为 CENTER_RIGHT 会将标题放在右边,但整个标题、文本和图形。

titledPane.setAlignment(Pos.CENTER_RIGHT);

我尝试在 TitledPane 图形、HBox、GridPane 中尝试使用不同的容器。设置增长、对齐、填充宽度等。均无效。

如果不设置人为的图形间隙,我看不到任何可行的解决方案,但在上面提到的问题中建议了 hack。 这是一个丑陋的黑客。 对于 Acrodion 中的多个 TitledPanes。但是 Button 并没有放在最后,标题右边还有更多空间。

primaryStage.setOnShown(e -> {
    for (TitledPane titledPane : panes) {
        Node titleRegion = titledPane.lookup(".title");
        Insets padding = ((StackPane) titleRegion).getPadding();
        double graphicWidth = titledPane.getGraphic().getLayoutBounds().getWidth();
        double arrowWidth = titleRegion.lookup(".arrow-button").getLayoutBounds().getWidth();
        double labelWidth = titleRegion.lookup(".text").getLayoutBounds().getWidth();
        double nodesWidth = graphicWidth + padding.getLeft() + padding.getRight() + arrowWidth + labelWidth;
        titledPane.graphicTextGapProperty().bind(titledPane.widthProperty().subtract(nodesWidth));
    }
});

最佳答案

我不确定我是否完全理解您的目标,但我相信您希望 TitledPane 同时具有 Label和一个 ButtonLabel左对齐和 Button右对齐,正确吗?

为了做到这一点,确实需要一些解决方法和使用 JavaFX 的容器。首先,无需为TitledPane设置标题。本身;我们将添加一个 Label添加到我们的图形中作为标题。

在下面的示例中,我们将使用 HBoxTitledPane 中保存我们想要的所有节点. HBox内,我们添加一个 Region设置为始终在 HBox 内增长.这迫使 Button一直向右。

然后,自TitledPane它的图形有一个半固定的最大宽度,我们需要绑定(bind)我们的 HBoxTitledPane 的宽度,带有填充以避免与“展开”箭头重叠。

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        // Simple interface
        VBox root = new VBox(5);
        root.setPadding(new Insets(10));
        root.setAlignment(Pos.CENTER);

        // Create the TitlePane
        TitledPane titledPane = new TitledPane();
        titledPane.setAlignment(Pos.CENTER);

        // Create HBox to hold our Title and button
        HBox contentPane = new HBox();
        contentPane.setAlignment(Pos.CENTER);

        // Set padding on the left side to avoid overlapping the TitlePane's expand arrow
        // We will also pad the right side
        contentPane.setPadding(new Insets(0, 10, 0, 35));

        // Now, since the TitlePane's graphic node generally has a fixed size, we need to bind our
        // content pane's width to match the width of the TitledPane. This will account for resizing as well
        contentPane.minWidthProperty().bind(titledPane.widthProperty());

        // Create a Region to act as a separator for the title and button
        HBox region = new HBox();
        region.setMaxWidth(Double.MAX_VALUE);
        HBox.setHgrow(region, Priority.ALWAYS);

        // Add our nodes to the contentPane
        contentPane.getChildren().addAll(
                new Label("Titles Are Cool"),
                region,
                new Button("Click me!")
        );

        // Add the contentPane as the graphic for the TitledPane
        titledPane.setGraphic(contentPane);

        // Add the pane to our root layout
        root.getChildren().add(titledPane);

        // Show the Stage
        primaryStage.setWidth(400);
        primaryStage.setHeight(300);
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }
}

The Result:

Screenshot

这是一个相当干净的解决方案,我不认为它是一个“hack”。仅按预期使用 JavaFX 的容器和结构。

关于JavaFX 11 : Add a graphic to a TitledPane on the right side,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52457813/

相关文章:

JavaFx Canvas : how to do XOR drawing?

java - 在 JavaFX 中使用 F11 键切换全屏功能

java - Apache tika 错误地检测 csv 的 mime 类型

java - 以管理员身份发布到 Facebook 页面

java - java中正则表达式中的可选元素

java - 没有找到对象时如何处理返回值

java - java中从二进制文件中读取对象

javafx - JavaFX FXML 模式不支持 Pane 内的上下文菜单的理由是什么?

java - 使用 JavaFX 手动触发 CheckComboBox 的下拉菜单

Java getResourceAsStream 返回 NullPointerException?