Table of contents
1.
Introduction
2.
VBox Class
2.1.
Constructors
2.2.
Common Methods
2.3.
Usage
3.
Implementation
3.1.
Program 1
3.2.
Program 2
4.
Frequently Asked Questions
4.1.
What is JavaFX used for?
4.2.
What is a VBox in JavaFX?
4.3.
What is Insets class in JavaFX?
5.
Conclusion
Last Updated: Mar 27, 2024
Medium

JavaFX VBox

Author Harsh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JavaFX is a software framework for developing and delivering desktop and rich web applications that can operate on a wide range of devices. JavaFX is compatible with Microsoft Windows, Linux, and macOS desktop PCs and web browsers, as well as iOS and Android mobile devices.

In this blog, we will discuss the VBox class present in the JavaFX framework. We will look into the different types of constructors and different methods present inside the VBox class. We will also discuss how we can use these different methods inside our code.

So, let's get started.

JavaFx

Source: Wikipedia

VBox Class

Vbox is a part of the JavaFX framework and is 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.

VBox in JavaFx

Now we’ll discuss the different constructors present in this class using which we can initiate the class.

Constructors

There are 4 different types of constructor available in vbox.

  1. VBox(): It is used to create a vbox layout having the spacing of 0 and aligned at TOP_LEFT.
     
  2. VBox(double spacing): Creates a VBox layout with the specified child spacing.
     
  3. VBox(double spacing, Node... children): Creates a new VBox with the provided number of nodes and space between them.
     
  4. VBox(Node... children): Creates a vbox layout with 0 spacing.
     

Common Methods

VBox Methods

Usage

Now, we know what is VBox, what are different functions available in it, and what are the different types of constructors available in it. Before jumping onto the implementation directly let’s first see where we use Vbox class.
In JavaFX, we have different layouts classes such as HBox, VBox, Border Pane, Stack Pane, Text Flow, Anchor Pane, etc. All of these layout panes are used to organize the nodes in a different order. And VBox is used to organize the nodes in vertical order.

Now, we know why we use VBox. let’s see the implementation.

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

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

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 JavaFXJavaFX Rotate TransitionJavaFX Scale TransitionJavaFX Fill Transition, and many more on our Website.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem 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 problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Happy Learning!

Live masterclass