TitledPane in JavaFX Controls - dummies

TitledPane in JavaFX Controls

By Doug Lowe

A titled pane in JavaFX is a pane that contains a single content node and draws a title bar and a border around the content to distinguish the content from other content in the scene.

In addition, title panes are collapsible, which means that the user can collapse the titled pane so that just the title bar is visible. However, you can make the titled pane non-collapsible if you wish.

In spite of its name, TitledPane is a control, not a layout pane. In other words, it is a subclass of Control, not of Pane.

The TitledPane class has a simple constructor that accepts a string that will be displayed in the title bar and a node that will be displayed as the content. The node can, of course, be a layout pane that contains child nodes.

Thus, a titled pane can contain multiple nodes. Here’s an example that creates three radio buttons, adds them to a toggle group and a VBox, and then creates a titled pane class to display the group of buttons:

RadioButton rbSmall = new RadioButton("Small");
RadioButton rbMedium = new RadioButton("Medium");
RadioButton rbLarge = new RadioButton("Large");
ToggleGroup sizeGroup = new ToggleGroup();
sizeGroup.getToggles().addAll(rbSmall, rbMedium, rbLarge);
VBox box = new VBox(10);
box.setPadding(new Insets(10));
box.getChildren().addAll(rbSmall, rbMedium, rbLarge);
TitledPane tpane = new TitledPane("Size", box);

If you want to make the titled pane non-collapsible, add the following line:


Then, the user can’t collapse the pane. In this case, the main purpose of the titled pane becomes visual: The border creates a visual grouping of the radio buttons, and the title bar lets the user know why these radio buttons are grouped (they let the user choose a size option).

This figure shows two titled panes: one collapsible, the other non-collapsible.

Titled pane controls in JavaFX.
Titled pane controls in JavaFX.