JavaFX Tutorial - JavaFX TitledPane








A titled pane is a panel with a title and the pane can be opened and closed. We can add Node such as UI controls or images, and groups of elements to the pane.

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
//from  w w w . j  a v  a 2 s  .  c o m
public class Main extends Application {
  public static void main(String[] args) {
    launch(args);
  }

  @Override
  public void start(Stage stage) {
    Scene scene = new Scene(new Group(), 350, 250);
    TitledPane titledPane = new TitledPane("My Title", new CheckBox("OK"));

    HBox hbox = new HBox(10);
    hbox.setPadding(new Insets(20, 0, 0, 20));
    hbox.getChildren().setAll(titledPane);

    Group root = (Group) scene.getRoot();
    root.getChildren().add(hbox);
    stage.setScene(scene);
    stage.show();
  }
}

The code above generates the following result.

null




Creating Titled Panes

To create a TitledPane control call its constructors.

The following code uses a two-parameter constructor from TitledPane. It names the titled pane as "My Pane" and fills the pane with a Button control.

TitledPane tp = new TitledPane("My Pane", new Button("Button"));

The next few lines did the same thing as the code above without using the constructor with parameters. It creates a TitledPane with default empty constructor and set the title and content control afterwards.

TitledPane tp = new TitledPane();
tp.setText("My Titled Pane");
tp.setContent(new Button("Button"));

The following code uses GridPane to layout controls inside a TitledPane.

TitledPane gridTitlePane = new TitledPane();
GridPane grid = new GridPane();
grid.setVgap(4);
grid.setPadding(new Insets(5, 5, 5, 5));
...
gridTitlePane.setText("Grid");
gridTitlePane.setContent(grid);

We can define the way a titled pane opens and closes. By default, all titled panes are collapsible and the open and close action are animated.

The setCollapsible(false) turns off the Collapsible state. setAnimated(false) stops the animation.

TitledPane tp = new TitledPane();
tp.setCollapsible(false);//remove closing action
tp.setAnimated(false);//stop animating

Full source code

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
/*from   ww w . j a  v a 2s  .  c  o  m*/
public class Main extends Application {
  public static void main(String[] args) {
    launch(args);
  }

  @Override
  public void start(Stage stage) {
    Scene scene = new Scene(new Group(), 450, 250);
    TitledPane titledPane = new TitledPane("My Title", new CheckBox("OK"));
    titledPane.setCollapsible(false);//remove closing action
    titledPane.setAnimated(false);//stop animating
    
    HBox hbox = new HBox(10);
    hbox.setPadding(new Insets(20, 0, 0, 20));
    hbox.getChildren().setAll(titledPane);

    Group root = (Group) scene.getRoot();
    root.getChildren().add(hbox);
    stage.setScene(scene);
    stage.show();
  }
}

The code above generates the following result.

null