We’ve collected these simple code snippets to allow you learn JavaFX.

Example 1: Titledpane

Study the following code:

Step 1: Create Project

  1. Open your favorite Java IDE.
  2. In the menu go to File --> Create New Project.

Step 2: Dependencies

No dependencies are needed for this project.

Step 3: Write Code

Our code will comprise the following java files:

  • TitledPaneExample.java
  1. In your editor or IDE, create a file known as TitledPaneExample.java.
  2. Then add the following code:

(a). TitledPaneExample.java

We will start by adding some imports to this class:

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;

Through inheritance we will be able to derive properties from a parent class. However we have to extend that parent class. So we do that using the extends keyword.

public class TitledPaneExample extends Application {

Our class will have the following methods:

  • void main(String[] args)
  • void start(Stage primaryStage)

All Java applications have an entry point known as the main method. We will define it:

    public static void main(String[] args) {

In this particular class we will be overriding our void start(Stage primaryStage) method.
Prepend the code>@Override</code modifier to your method. Then add implementation code as follows:

    @Override
    public void start(Stage primaryStage) {
        Label label = new Label("The content inside the TitledPane");
        TitledPane titledPane = new TitledPane("The Title", label);
        titledPane.setCollapsible(false);

        titledPane.setExpanded(true);
        titledPane.setExpanded(false);

        Scene scene = new Scene(new VBox(titledPane));
        primaryStage.setScene(scene);

        primaryStage.show();
    }

Here is the full code:

package com.jenkov.javafx.titledpane;

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 {

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

    @Override
    public void start(Stage primaryStage) {
        Label label = new Label("The content inside the TitledPane");
        TitledPane titledPane = new TitledPane("The Title", label);
        titledPane.setCollapsible(false);

        titledPane.setExpanded(true);
        titledPane.setExpanded(false);

        Scene scene = new Scene(new VBox(titledPane));
        primaryStage.setScene(scene);

        primaryStage.show();
    }
}

Download

Download the code using the below links:

Number Link
1. Download Example
2. Follow code author
3. Code: Apache 2.0 License