JavaFX VBox Example Tutorial

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

Instead of arranging the nodes in a horizontal row, VBox Layout Pane arranges the nodes in a single vertical column. It is represented by javafx.scene.layout.VBox class which provides all the methods to deal with the styling and the distance among the nodes. This class needs to be instantiated in order to implement a VBox layout in our application.

Complete JavaFX tutorial at https://www.javaguides.net/p/javafx-tutorial.html.

JavaFX VBox Example

The 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