JavaFX Tutorial - JavaFX Separator








The Separator class represents a horizontal or vertical separator line. It divides elements and does not produce any action.

We can style, apply visual effects, and animate a separator.

By default, the separator is horizontal. We can change its orientation by using the setOrientation method.

The Separator class extends the Node class.

Creating a Separator

To create a horizontal separator

Separator separator1 = new Separator();

To create a vertical separator

Separator separator2 = new Separator();
separator2.setOrientation(Orientation.VERTICAL);

The setMaxWidth method defines a particular width.

The setValignment method specifies the vertical position.

Example

Label with separator

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.VPos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;
/*w ww . ja v  a2s .  c om*/
public class Main extends Application {

    Label caption = new Label("We");

    @Override
    public void start(Stage stage) {
        Group root = new Group();
        Scene scene = new Scene(root, 500, 200);
        stage.setScene(scene);

        GridPane grid = new GridPane();
        grid.setPadding(new Insets(10, 10, 10, 10));
        grid.setVgap(2);
        grid.setHgap(5);

        scene.setRoot(grid);

        caption.setFont(Font.font("Verdana", 20));

        GridPane.setConstraints(caption, 0, 0);
        GridPane.setColumnSpan(caption, 8);
        grid.getChildren().add(caption);

        final Separator sepHor = new Separator();
        sepHor.setValignment(VPos.CENTER);
        GridPane.setConstraints(sepHor, 0, 1);
        GridPane.setColumnSpan(sepHor, 7);
        grid.getChildren().add(sepHor);

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

The code above generates the following result.

null