JavaFX Titled Pane

0

JavaFX Titled Pane Tutorials:

The Titled Pane is a panel with title and this panel can be opened and closed.

We can add any node to the pane so it is used to encapsulate nodes.

Constructors :

  • TitledPane()Creates a new TitledPane with no title or content.
  • TitledPane(String title, Node content)Creates a new TitledPane with a title and content.

Creating Titled Pane :

TitledPane titledPane = new TitledPane();

Create a button

Button button =new Button(" NEW BUTTON ");

Add button to the titledPane

titledPane.setContent(button);

titledPane.setExpanded(true);

Sets the expanded state of the TitledPane.

Example using titled pane:


package javafxtuts;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Javafxtuts extends Application {

  public static void main(String[] args) {
     Application.launch(args);
  }

  @Override
  public void start(Stage stage) {
      stage.setTitle("Javafxtuts.com");
      
      TitledPane titledPane = new TitledPane();
      //to create a new button 
       Button button =new Button(" NEW BUTTON ");
        titledPane.setContent(button);
        // to Sets the expanded state of the TitledPane
        titledPane.setExpanded(true);

      VBox root =new VBox();
      root.setPadding(new Insets(20, 10, 10, 10));
      root.getChildren().addAll(titledPane);
        Scene scene = new Scene(root, 250, 200);
        stage.setScene(scene);
        stage.show();
  }
}

Output :

Explanation of titled pane example :

  • Creating a new TitledPane asTitledPane titledPane = new TitledPane();
  • Button button =new Button(" NEW BUTTON ");It create a new button.
  • Set the node using setContent() to the TitledPane() AStitledPane.setContent(button);
  • titledPane.setExpanded(true);It expands the titledPane.
Share.

Leave A Reply