difference between TitledPane and Accordion
Here’s a clear explanation of the difference between TitledPane and Accordion in JavaFX:
| Feature | TitledPane | Accordion |
|---|---|---|
| Purpose | A single collapsible/expandable panel with a title and content. | A container that manages multiple TitledPanes and allows only one pane to be expanded at a time. |
| Number of Sections | One. | Multiple. |
| Behavior | Can expand/collapse independently. | Only one pane is expanded at a time; expanding one collapses others automatically. |
| Use Case | Standalone collapsible section, like a single settings panel. | Group of collapsible sections, like a multi-section settings page or FAQ. |
| Children | Contains any Node as content. | Contains multiple TitledPanes as children. |
Example: TitledPane Alone
TitledPane tp = new TitledPane("Settings", new Label("Option 1"));
tp.setExpanded(false); // initially collapsed
- Simple collapsible panel.
- Independent; you can have multiple TitledPanes, but they expand/collapse independently.
Example: Accordion with Multiple TitledPanes
TitledPane tp1 = new TitledPane("Settings", new Label("Option 1"));
TitledPane tp2 = new TitledPane("Profile", new Label("Option 2"));
TitledPane tp3 = new TitledPane("About", new Label("Option 3"));
Accordion accordion = new Accordion();
accordion.getPanes().addAll(tp1, tp2, tp3);
- Only one TitledPane is open at a time.
- Expanding
Profilewill automatically collapseSettingsif it was open.
💡 Rule of Thumb
- Use TitledPane if you need just one collapsible section.
- Use Accordion if you want multiple sections with exclusive expansion.