Updated March 18, 2023
Introduction to JavaFX VBox
In JavaFX, VBox is a component that lays out its children node in a vertical fashion. If it has a padding set or border, contents will be displayed within that insets. Moreover, on adding new children, they are placed below the previous child node. If no height and width are mentioned, VBox will consider the default size as children’s width and height and align it to the top-left position. VBox class is extended by the Pane class and can be instantiated from the class javafx.scene.layout.VBox.
Constructors
To implement the VBox in Java, four constructors can be used.
- VBox(): A layout will be created with 0 spacing.
- VBox(Double spacing): A layout will be created with the spacing specified.
- VBox(Double spacing, Node children): A layout will be created with the spacing and children nodes specified.
- VBox(Node? children): A layout will be created with the spacing 0 and children nodes specified.
Properties
JavaFX VBox has three properties such as,
- alignment: Property that is used for the alignment of the children that is within the height and width of VBox.
- fillWidth: Resizable children nodes will be resized to the VBox width or kept in the preferred width on setting this property as true.
- spacing: Vertical space between the child nodes in vbox will be set.
Top 15 Methods of JavaFX VBox
Below are mentioned some commonly used methods in JavaFX VBox:
- clearConstraints(Node child): VBox constraints will be removed from the child node.
- computeMinWidth(double height): The minimum width of the region will be computed using this method.
- computeMinHeight(double width): The minimum height of the region will be computed using this method.
- getAlignment(): alignment property’s value will be returned.
- isFillWidth(): fillWidth property’s value will be returned.
- setAlignment(Pos value): alignment property’s value will be set.
- setSpacing(double value): spacing property’s value will be set.
- getMargin(Node child): margin property’s value will be returned.
- getSpacing (): spacing property’s value will be returned.
- getVgrow(Node child): vgrow property’s value will be returned.
- setFillWidth (boolean value): fillWidth property’s value will be set.
- spacingProperty(): The vertical space between the child nodes in vbox will be set.
- setMargin(Nodechild, Insets value): The margin for the child will be set in the VBox.
- computePrefWidth(double height): A preferred width for the region that is needed for the given height will be computed.
- computePrefHeight(double width): A preferred height for the region that is needed for the given width will be computed.
Programs to Implement JavaFX VBox
Now, let us see some examples of VBox with some of the methods explained in the above section.
Example #1
Code:
//Java program to create a VBox
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
//class that extends Application base class
public class JavaFXVBoxExample extends Application {
//application starts at this point
@Override
public void start(Stage s) throws Exception {
//create button 1
Button b1 = new Button("Sample button 1 for VBox");
Button b2 = new Button("Sample button 2 for VBox");
//create vbox
VBox vb = new VBox();
//create scene
Scene sc = new Scene(vb,200,200);
//add them
vb.getChildren().addAll(b1,b2);
//set the scene
s.setScene(sc);
//display the result
s.show();
}
//main method
public static void main(String[] args) {
launch(args);
}
}
Output:
- Firstly, 2 buttons b1 and b2 are created with the text ‘Sample button 1 for VBox’ and ‘Sample button 2 for VBox’.
- Then, create a VBox and a scene.
- After setting the scene, the results will be displayed.
Example #2
Code:
//Java program to demonstrate JavaFX VBox with center alignment
import 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;
//class that extends Application class
public class JavaFXVBoxExample extends Application {
// application starts at this point
public void start(Stage s)
{
try {
// set title
s.setTitle("VBox Example");
// create VBox
VBox vb = new VBox(10);
// create label
Label lb = new Label("Hey ... This is the sample for JavaFX VBox !!");
// add the created label to vbox
vb.getChildren().add(lb);
// set alignment of the vbox
vb.setAlignment(Pos.CENTER);
// add buttons to VBox
for (int i = 0; i < 4; i++)
{
vb.getChildren().add(new Button("This is the sample button " + (int)(i + 1)));
}
// create a scene
Scene sc = new Scene(vb, 400, 300);
// set the scene
s.setScene(sc);
s.show();
}
//catch the exception
catch (Exception e) {
System.out.println(e.getMessage());
}
}
// Main Method
public static void main(String args[])
{
// launch the application
launch(args);
}
}
Output:
- Firstly, create a VBox and a label.
- Then, add the created label to VBox.
- Create buttons using a for loop so that button creation syntax needs not written again.
- Create a scene and set it.
- After setting the scene, buttons will be displayed in the center.
Example #3
Code:
//Java program to create VBox with padding and insets
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
public class JavaFXVBoxExample extends Application {
//main method
public static void main(String[] args) {
Application.launch(args);
}
//application starts at this point
@Override
public void start(Stage s) {
//set title
s.setTitle("VBox Sample");
// Create VBox
VBox vb = new VBox();
//set padding
vb.setPadding(new Insets(10, 50, 50, 50));
//set spacing
vb.setSpacing(10);
//create label
Label lbl = new Label("This is the VBox");
//set font properties
lbl.setFont(Font.font("Amble CN", FontWeight.BOLD, 24));
vb.getChildren().add(lbl);
// create Buttons
Button b1 = new Button();
b1.setText(" I am Button1");
vb.getChildren().add(b1);
Button b2 = new Button();
b2.setText("I am Button2");
vb.getChildren().add(b2);
Button b3 = new Button();
b3.setText("I am Button3");
vb.getChildren().add(b3);
Button b4 = new Button();
b4.setText("I am Button4");
vb.getChildren().add(b4);
// Add VBox to the scene that is created
Scene sc = new Scene(vb);
s.setScene(sc);
s.show();
}
}
Output:
- Firstly, A VBox is created with padding and insets set.
- Then, create a label with a customized font.
- After that, Create 4 buttons and a scene.
- At last, set the scene and display the results.
Conclusion
JavaFX VBox is a component that helps the child nodes to layout in a vertical manner. Also, the new child will be added next to the previous child similar to a stack. More details on JavaFX VBox is discussed in this document.
Recommended Articles
This is a guide to JavaFX VBox. Here we discuss a brief overview and its constructors with the top 3 methods and program implementation. You may also look at the following articles to learn more-