JavaFX VBox Example

In this tutorial, we will learn how to use the JavaFX VBox layout in the JavaFX application.

JavaFX VBox

The JavaFX VBox component is a layout component that positions all its child nodes (components) in a vertical column - on top of each other. 

The JavaFX VBox component is represented by the class javafx.scene.layout.VBox

Read more at https://docs.oracle.com/javase/10/docs/api/javafx/scene/layout/VBox.html

JavaFX VBox Example

The below JavaFX example shows six buttons in a single vertical column:

package com.javafx.examples.layout;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class VBoxExample extends Application {

    @Override
    public void start(Stage stage) {

        initUI(stage);
    }

    private void initUI(Stage stage) {

        VBox root = new VBox(5);
        root.setPadding(new Insets(10));

        Button prevBtn = new Button("Previous");
        Button nextBtn = new Button("Next");
        Button cancBtn = new Button("Cancel");
        Button helpBtn = new Button("Help");
        Button exitBtn = new Button("Exit");
        Button stopBtn = new Button("Stop");

        root.getChildren().addAll(prevBtn, nextBtn, cancBtn, helpBtn, exitBtn, stopBtn);

        Scene scene = new Scene(root, 200, 200);
        stage.setTitle("showing buttons in vertical column");
        stage.setScene(scene);
        stage.show();
    }

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

Let's understand the above JavaFX program.

A VBox pane is created with some spacing:

VBox root = new VBox(5);

We create some padding around the VBox:

root.setPadding(new Insets(10));

The buttons are added to the container:

root.getChildren().addAll(prevBtn, nextBtn, cancBtn, helpBtn, exitBtn, stopBtn);

We created six buttons:

        Button prevBtn = new Button("Previous");
        Button nextBtn = new Button("Next");
        Button cancBtn = new Button("Cancel");
        Button helpBtn = new Button("Help");
        Button exitBtn = new Button("Exit");
        Button stopBtn = new Button("Stop");

Output


Comments