JavaFX | VBox Class

06 Sep, 2018
VBox is a part of JavaFX. VBox lays out its children in form of vertical columns. If the vbox has a border and/or padding set, then the contents will be layed out within those insets. VBox class extends Pane class.
Constructor of the class:
VBox(): Creates a VBox layout with spacing = 0 and alignment at TOP_LEFT.VBox(double s): Creates a new VBox with specified spacing between children.VBox(double s, Node... c): Creates a new VBox with specified nodes and spacing between them.VBox(Node... c): Creates an VBox layout with spacing = 0.
VBox(): Creates a VBox layout with spacing = 0 and alignment at TOP_LEFT.
VBox(double s): Creates a new VBox with specified spacing between children.
VBox(double s, Node... c): Creates a new VBox with specified nodes and spacing between them.
VBox(Node... c): Creates an VBox layout with spacing = 0.
Commonly Used Methods:
Below programs illustrate the use of VBox class:
Java Program to create a VBox and add it to the stage: In this program we will create a VBox named vbox. We will create a label and add it to the vbox. We will also create some buttons and add them to the VBox using the getChildren().add() function. Now create a scene and add the vbox to the scene and add the scene to the stage and call show() function to display the final results.// Java Program to create a VBox // and add it to the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.canvas.*;import javafx.scene.web.*;import javafx.scene.Group;  public class VBOX_1 extends Application {      // launch the application    public void start(Stage stage)    {          try {              // set title for the stage            stage.setTitle("VBox");              // create a VBox            VBox vbox = new VBox();              // create a label            Label label = new Label("this is VBox example");              // add label to vbox            vbox.getChildren().add(label);              // add buttons to VBox            for (int i = 0; i < 10; i++)            {                vbox.getChildren().add(new Button("Button " + (int)(i + 1)));            }              // create a scene            Scene scene = new Scene(vbox, 300, 300);              // set the scene            stage.setScene(scene);              stage.show();        }          catch (Exception e) {              System.out.println(e.getMessage());        }    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}Output:Java Program to create a VBox, add spaces between its elements and add it to the stage: In this program we will create a VBox named vbox. We will set the spacing by passing a double value of space as an argument to the constructor. Now create a label and add it to the vbox. To add some buttons to the VBox use the getChildren().add() function. Finally, create a scene and add the vbox to the scene and add the scene to the stage and call show() function to display the final results.// Java Program to create a VBox, add // spaces between its elements and add// it to the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.canvas.*;import javafx.scene.web.*;import javafx.scene.Group;  public class VBOX_2 extends Application {      // launch the application    public void start(Stage stage)    {          try {              // set title for the stage            stage.setTitle("VBox");              // create a VBox            VBox vbox = new VBox(10);              // create a label            Label label = new Label("this is VBox example");              // add label to vbox            vbox.getChildren().add(label);              // add buttons to VBox            for (int i = 0; i < 5; i++)            {                vbox.getChildren().add(new Button("Button " + (int)(i + 1)));            }              // create a scene            Scene scene = new Scene(vbox, 300, 300);              // set the scene            stage.setScene(scene);              stage.show();        }          catch (Exception e) {              System.out.println(e.getMessage());        }    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}Output:Java Program to create a VBox, add spaces between its elements, set an alignment and add it to the stage: In this program we will create a VBox named vbox. We will set the spacing by passing a double value of space as an argument to the constructor. Set the alignment of the VBox using the setAlignment() function. Then create a label and add it to the vbox. Add some buttons to the VBox using the getChildren().add() function. Finally, create a scene and add the vbox to the scene and add the scene to the stage and call show() function to display the final results.// Java Program to create a VBox, add spaces// between its elements, set an alignment // and add it to the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.Group;import javafx.geometry.Pos;  public class VBOX_3 extends Application {      // launch the application    public void start(Stage stage)    {          try {              // set title for the stage            stage.setTitle("VBox");              // create a VBox            VBox vbox = new VBox(10);              // create a label            Label label = new Label("this is VBox example");              // add label to vbox            vbox.getChildren().add(label);              // set alignment            vbox.setAlignment(Pos.CENTER);              // add buttons to VBox            for (int i = 0; i < 5; i++)            {                vbox.getChildren().add(new Button("Button " + (int)(i + 1)));            }              // create a scene            Scene scene = new Scene(vbox, 300, 300);              // set the scene            stage.setScene(scene);              stage.show();        }          catch (Exception e) {              System.out.println(e.getMessage());        }    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}Output:
Java Program to create a VBox and add it to the stage: In this program we will create a VBox named vbox. We will create a label and add it to the vbox. We will also create some buttons and add them to the VBox using the getChildren().add() function. Now create a scene and add the vbox to the scene and add the scene to the stage and call show() function to display the final results.// Java Program to create a VBox // and add it to the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.canvas.*;import javafx.scene.web.*;import javafx.scene.Group;  public class VBOX_1 extends Application {      // launch the application    public void start(Stage stage)    {          try {              // set title for the stage            stage.setTitle("VBox");              // create a VBox            VBox vbox = new VBox();              // create a label            Label label = new Label("this is VBox example");              // add label to vbox            vbox.getChildren().add(label);              // add buttons to VBox            for (int i = 0; i < 10; i++)            {                vbox.getChildren().add(new Button("Button " + (int)(i + 1)));            }              // create a scene            Scene scene = new Scene(vbox, 300, 300);              // set the scene            stage.setScene(scene);              stage.show();        }          catch (Exception e) {              System.out.println(e.getMessage());        }    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}Output:
// Java Program to create a VBox // and add it to the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.canvas.*;import javafx.scene.web.*;import javafx.scene.Group;  public class VBOX_1 extends Application {      // launch the application    public void start(Stage stage)    {          try {              // set title for the stage            stage.setTitle("VBox");              // create a VBox            VBox vbox = new VBox();              // create a label            Label label = new Label("this is VBox example");              // add label to vbox            vbox.getChildren().add(label);              // add buttons to VBox            for (int i = 0; i < 10; i++)            {                vbox.getChildren().add(new Button("Button " + (int)(i + 1)));            }              // create a scene            Scene scene = new Scene(vbox, 300, 300);              // set the scene            stage.setScene(scene);              stage.show();        }          catch (Exception e) {              System.out.println(e.getMessage());        }    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}
Output:
Java Program to create a VBox, add spaces between its elements and add it to the stage: In this program we will create a VBox named vbox. We will set the spacing by passing a double value of space as an argument to the constructor. Now create a label and add it to the vbox. To add some buttons to the VBox use the getChildren().add() function. Finally, create a scene and add the vbox to the scene and add the scene to the stage and call show() function to display the final results.// Java Program to create a VBox, add // spaces between its elements and add// it to the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.canvas.*;import javafx.scene.web.*;import javafx.scene.Group;  public class VBOX_2 extends Application {      // launch the application    public void start(Stage stage)    {          try {              // set title for the stage            stage.setTitle("VBox");              // create a VBox            VBox vbox = new VBox(10);              // create a label            Label label = new Label("this is VBox example");              // add label to vbox            vbox.getChildren().add(label);              // add buttons to VBox            for (int i = 0; i < 5; i++)            {                vbox.getChildren().add(new Button("Button " + (int)(i + 1)));            }              // create a scene            Scene scene = new Scene(vbox, 300, 300);              // set the scene            stage.setScene(scene);              stage.show();        }          catch (Exception e) {              System.out.println(e.getMessage());        }    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}Output:
// Java Program to create a VBox, add // spaces between its elements and add// it to the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.canvas.*;import javafx.scene.web.*;import javafx.scene.Group;  public class VBOX_2 extends Application {      // launch the application    public void start(Stage stage)    {          try {              // set title for the stage            stage.setTitle("VBox");              // create a VBox            VBox vbox = new VBox(10);              // create a label            Label label = new Label("this is VBox example");              // add label to vbox            vbox.getChildren().add(label);              // add buttons to VBox            for (int i = 0; i < 5; i++)            {                vbox.getChildren().add(new Button("Button " + (int)(i + 1)));            }              // create a scene            Scene scene = new Scene(vbox, 300, 300);              // set the scene            stage.setScene(scene);              stage.show();        }          catch (Exception e) {              System.out.println(e.getMessage());        }    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}
Output:
Java Program to create a VBox, add spaces between its elements, set an alignment and add it to the stage: In this program we will create a VBox named vbox. We will set the spacing by passing a double value of space as an argument to the constructor. Set the alignment of the VBox using the setAlignment() function. Then create a label and add it to the vbox. Add some buttons to the VBox using the getChildren().add() function. Finally, create a scene and add the vbox to the scene and add the scene to the stage and call show() function to display the final results.// Java Program to create a VBox, add spaces// between its elements, set an alignment // and add it to the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.Group;import javafx.geometry.Pos;  public class VBOX_3 extends Application {      // launch the application    public void start(Stage stage)    {          try {              // set title for the stage            stage.setTitle("VBox");              // create a VBox            VBox vbox = new VBox(10);              // create a label            Label label = new Label("this is VBox example");              // add label to vbox            vbox.getChildren().add(label);              // set alignment            vbox.setAlignment(Pos.CENTER);              // add buttons to VBox            for (int i = 0; i < 5; i++)            {                vbox.getChildren().add(new Button("Button " + (int)(i + 1)));            }              // create a scene            Scene scene = new Scene(vbox, 300, 300);              // set the scene            stage.setScene(scene);              stage.show();        }          catch (Exception e) {              System.out.println(e.getMessage());        }    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}Output:
// Java Program to create a VBox, add spaces// between its elements, set an alignment // and add it to the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.Group;import javafx.geometry.Pos;  public class VBOX_3 extends Application {      // launch the application    public void start(Stage stage)    {          try {              // set title for the stage            stage.setTitle("VBox");              // create a VBox            VBox vbox = new VBox(10);              // create a label            Label label = new Label("this is VBox example");              // add label to vbox            vbox.getChildren().add(label);              // set alignment            vbox.setAlignment(Pos.CENTER);              // add buttons to VBox            for (int i = 0; i < 5; i++)            {                vbox.getChildren().add(new Button("Button " + (int)(i + 1)));            }              // create a scene            Scene scene = new Scene(vbox, 300, 300);              // set the scene            stage.setScene(scene);              stage.show();        }          catch (Exception e) {              System.out.println(e.getMessage());        }    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}
Output:
Note: The above programs might not run in an online IDE please use an offline compiler.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/VBox.html
JavaFX
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Interfaces in Java
Stream In Java
Collections in Java
Singleton Class in Java
Set in Java
Introduction to Java
Constructors in Java
Multithreading in Java
Initializing a List in Java
Exceptions in Java