TitledPane
In JavaFX, a TitledPane is a container with a title bar that can collapse or expand its content.
- It’s like a single collapsible panel.
- Useful for accordion-style interfaces or settings panels.
Basic Example
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class TitledPaneExample extends Application {
@Override
public void start(Stage stage) {
// Content inside the TitledPane
VBox content = new VBox();
content.getChildren().addAll(
new Label("Option 1"),
new Label("Option 2"),
new Label("Option 3")
);
// Create TitledPane
TitledPane titledPane = new TitledPane("Settings", content);
titledPane.setCollapsible(true); // can collapse/expand
titledPane.setExpanded(false); // initially collapsed
// Add TitledPane to scene
VBox root = new VBox(titledPane);
root.setSpacing(10);
Scene scene = new Scene(root, 300, 200);
stage.setTitle("TitledPane Example");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
Result:
- Shows a panel with the title “Settings”.
- Clicking the title expands or collapses the content.
Key Methods & Properties
titledPane.setText("Title"); // set title
titledPane.setContent(Node); // set content inside
titledPane.setCollapsible(true); // allow collapse/expand
titledPane.setExpanded(true/false); // initially expanded or collapsed
FXML Version
<TitledPane xmlns:fx="http://javafx.com/fxml" text="Settings" collapsible="true" expanded="false">
<content>
<VBox spacing="5">
<children>
<Label text="Option 1"/>
<Label text="Option 2"/>
<Label text="Option 3"/>
</children>
</VBox>
</content>
</TitledPane>
Use Cases
- Settings panel (expand/collapse sections)
- FAQ sections (question = title, answer = content)
- Accordion interfaces (multiple TitledPanes stacked)
- Optional detailed info for forms
💡 Tip:
If you have multiple TitledPanes, you can use an Accordion so that only one pane is expanded at a time:
Accordion accordion = new Accordion();
accordion.getPanes().addAll(titledPane1, titledPane2, titledPane3);
- This is perfect for a settings or dashboard panel where you don’t want all sections open simultaneously.