layoutjavafx-2groupboxtitled-border

GroupBox / TitledBorder in JavaFX 2?


Is there something like a GroupBox or TitledBorder available on JavaFX 2?

Thanks for any hint :-)


Solution

  • Unless you need the custom styling in this answer, I prefer the TitledPane with setCollapsible(false) solution by Andriy Kryvtsun. For use, see a TitledPane tutorial.


    No such exact standard control, but it it is easy to create your own. Here is a sample implementation:

    /** Places content in a bordered pane with a title. */
    class BorderedTitledPane extends StackPane {
      BorderedTitledPane(String titleString, Node content) {
        Label title = new Label(" " + titleString + " ");
        title.getStyleClass().add("bordered-titled-title");
        StackPane.setAlignment(title, Pos.TOP_CENTER);
     
        StackPane contentPane = new StackPane();
        content.getStyleClass().add("bordered-titled-content");
        contentPane.getChildren().add(content);
     
        getStyleClass().add("bordered-titled-border");
        getChildren().addAll(title, contentPane);
      }
    }
    

    And the accompanying css for it:

    .label {
      -fx-font: 28px Vivaldi;
    }
    
    .bordered-titled-title {
      -fx-background-color: white;
      -fx-translate-y: -16;
    }
     
    .bordered-titled-border {
      -fx-content-display: top;
      -fx-border-insets: 20 15 15 15;
      -fx-background-color: white;
      -fx-border-color: black;
      -fx-border-width: 2;
    }
     
    .bordered-titled-content {
      -fx-padding: 26 10 10 10;
    }
    

    The code is from a example I created in response to an Oracle JavaFX forum thread post "Equivalent to BorderFactory.createTitledBorder".

    The output of the example program is as shown below.

    gettysburg