JavaFX | VBox Class
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.
Commonly Used Methods:
| Method | Explanation |
|---|---|
| getAlignment() | Returns the value of property alignment. |
| getSpacing() | Returns the spacing between its children. |
| setAlignment(Pos value) | Sets the Alignment of the VBox. |
| getChildren() | Returns the nodes in the VBox. |
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 stageimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.canvas.*;importjavafx.scene.web.*;importjavafx.scene.Group;publicclassVBOX_1extendsApplication {// launch the applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("VBox");// create a VBoxVBox vbox =newVBox();// create a labelLabel label =newLabel("this is VBox example");// add label to vboxvbox.getChildren().add(label);// add buttons to VBoxfor(inti =0; i <10; i++){vbox.getChildren().add(newButton("Button "+ (int)(i +1)));}// create a sceneScene scene =newScene(vbox,300,300);// set the scenestage.setScene(scene);stage.show();}catch(Exception e) {System.out.println(e.getMessage());}}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(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 stageimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.canvas.*;importjavafx.scene.web.*;importjavafx.scene.Group;publicclassVBOX_2extendsApplication {// launch the applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("VBox");// create a VBoxVBox vbox =newVBox(10);// create a labelLabel label =newLabel("this is VBox example");// add label to vboxvbox.getChildren().add(label);// add buttons to VBoxfor(inti =0; i <5; i++){vbox.getChildren().add(newButton("Button "+ (int)(i +1)));}// create a sceneScene scene =newScene(vbox,300,300);// set the scenestage.setScene(scene);stage.show();}catch(Exception e) {System.out.println(e.getMessage());}}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(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 stageimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.Group;importjavafx.geometry.Pos;publicclassVBOX_3extendsApplication {// launch the applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("VBox");// create a VBoxVBox vbox =newVBox(10);// create a labelLabel label =newLabel("this is VBox example");// add label to vboxvbox.getChildren().add(label);// set alignmentvbox.setAlignment(Pos.CENTER);// add buttons to VBoxfor(inti =0; i <5; i++){vbox.getChildren().add(newButton("Button "+ (int)(i +1)));}// create a sceneScene scene =newScene(vbox,300,300);// set the scenestage.setScene(scene);stage.show();}catch(Exception e) {System.out.println(e.getMessage());}}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(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
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


