Implementation
We’ll have a look at two different programs in which we’ll implement vbox. In the first program, we’ll create a very basic application in which we’ll add two buttons and a label with custom text just to get the gist of how VBox works. And in the second program we’ll update our current program and we’ll add a text field in which user can enter some text. Also, we’ll update the text of our buttons and add some margin using the inbuilt functions available in VBox class.
Let’s see the implementation.
Program 1
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.geometry.Pos;
public class VBox_Example extends Application {
@Override
public void start(Stage stage) {
try{
// setting the title
stage.setTitle("VBox Example");
// creating VBox
VBox vbox = new VBox(10);
// creating a scene
Scene scene = new Scene(vbox, 400, 400);
// creating two buttons
Button submit = new Button("Button 1");
Button clear = new Button("Button 2");
// creating label
Label label = new Label("VBox Example ( Two Buttons ) ");
// adding label to vbox
vbox.getChildren().add(label);
// adding two buttons
vbox.getChildren().add(submit);
vbox.getChildren().add(clear);
// setting alignment
vbox.setAlignment(Pos.CENTER_LEFT);
// setting the scene
stage.setScene(scene);
stage.show();
} catch (Exception e){
e.printStackTrace();
}
}
public static void main(String args[]){
// launching the application
launch(args);
}
}

You can also try this code with Online Java Compiler
Run Code
Output

Program 2
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
public class VBox_Example extends Application {
@Override
public void start(Stage stage) {
try{
// setting the title
stage.setTitle("VBox Example");
// creating VBox
VBox vbox = new VBox(10);
// creating a scene
Scene scene = new Scene(vbox, 400, 400);
//creating a text field
TextField textField = new TextField();
// creating two buttons
Button submit = new Button("Submit");
Button clear = new Button("Clear");
// creating label
Label label = new Label("VBox Example (Two Buttons and Textfield) ");
// adding label to vbox
vbox.getChildren().add(label);
// adding textfield
vbox.getChildren().add(textField);
// adding two buttons
vbox.getChildren().add(submit);
vbox.getChildren().add(clear);
// setting alignment
vbox.setAlignment(Pos.CENTER_LEFT);
// setting margin
vbox.setMargin(label, new Insets(0, 0, 0, 25));
vbox.setMargin(textField, new Insets(0, 50, 0, 25));
vbox.setMargin(submit, new Insets(0, 0, 0, 25));
vbox.setMargin(clear, new Insets(0, 0, 0, 25));
// setting the scene
stage.setScene(scene);
stage.show();
} catch (Exception e){
e.printStackTrace();
}
}
public static void main(String args[]){
// launching the application
launch(args);
}
}

You can also try this code with Online Java Compiler
Run Code
Output

Frequently Asked Questions
What is JavaFX used for?
Rich client applications can be created, tested, debugged, and deployed using JavaFX, a set of graphics and media packages, across a variety of platforms..
What is a VBox in JavaFX?
It is a class present inside the javafx.scene.layout package. Vbox is used as the layout in our application and the children of vbox are arranged in the vertical column.
What is Insets class in JavaFX?
JavaFX includes the Insets class. The inside offsets for the four sides of a rectangular region are stored in the Insets class.
Conclusion
In this article, we have extensively discussed the VBox Class present inside JavaFx Framework. We have seen different constructors to initialize the VBox object. Also, we have seen different methods available inside the class along with the implementation.
If you think this blog has helped you enhance your knowledge about JavaFx Vbox and if you would like to learn more, check out our articles JavaFX, JavaFX Rotate Transition, JavaFX Scale Transition, JavaFX Fill Transition, and many more on our Website.
Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and Algorithms, Competitive Programming, JavaScript, System Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the problems, interview experiences, and interview bundle for placement preparations.
Nevertheless, you may consider our paid courses to give your career an edge over others!
Happy Learning!