We’ve prepared these code snippets to help you learn JavaFX.

Example 1: Separator

Here are the code snippets:

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:

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

(a). SeparatorExample.java

We will need to add a bunch of imports in our class:

import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

The next step is to extend our class as shown below:

public class SeparatorExample extends Application {

Our class will have the following methods:

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

The starting point of our Java app will be main method which we create as follows:

    public static void main(String[] args) {

Here is the full code:

package com.jenkov.javafx.separator;

import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class SeparatorExample extends Application {

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

    public void start(Stage primaryStage) {

        Label label1 = new Label("Component 1");
        Label label2 = new Label("Component 2");

        Separator separator = new Separator(Orientation.VERTICAL);

        VBox vBox = new VBox(label1, separator, label2);
        Scene scene = new Scene(vBox);

        primaryStage.setScene(scene);
        primaryStage.setTitle("JavaFX App");

        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