JavaFX Tutorial - JavaFX VBox








The VBox layout places child nodes stacked in a vertical column.

The new added children are placed beneath the previous child node.

By default the VBox honors the children's preferred width and height.

When the parent node is not resizable, for example a Group node, the maximum vertical column's width is based on the node with the greatest preferred width.

By default each child node aligns to the top-left (Pos.TOP_LEFT) position.

Example

The following code sets a TextArea control to grow vertically when the parent VBox's height is resized:

TextArea  myTextArea = new TextArea();
VBox.setHgrow(myTextArea,  Priority.ALWAYS);


import javafx.application.Application;<!-- w  w  w.  j av a  2  s.  co  m-->
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Main extends Application {
  @Override
  public void start(Stage primaryStage) {
    TextArea myTextArea = new TextArea();
    VBox hbox = new VBox();
    hbox.getChildren().add(myTextArea);
    VBox.setVgrow(myTextArea, Priority.ALWAYS);
    
    Scene scene = new Scene(hbox, 320, 112, Color.rgb(0, 0, 0, 0));
    primaryStage.setScene(scene);
    primaryStage.show();
  }

  public static void main(String[] args) {
    launch(args);
  }
}

The code above generates the following result.

null




Example 2

The following code uses four rectangles to demonstrate the use of a VBox.

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
/*from  ww  w  .jav  a 2  s  .c  om*/
public class Main extends Application {
  @Override
  public void start(Stage primaryStage) {
    Group root = new Group();
    Scene scene = new Scene(root, 300, 250);
    // 5 pixels space between child nodes
    VBox vbox = new VBox(5);
    // 1 pixel padding between child nodes only
    vbox.setPadding(new Insets(1));
    Rectangle r1 = new Rectangle(10, 10);
    Rectangle r2 = new Rectangle(20, 100);
    Rectangle r3 = new Rectangle(50, 20);
    Rectangle r4 = new Rectangle(20, 50);

    HBox.setMargin(r1, new Insets(2, 2, 2, 2));

    vbox.getChildren().addAll(r1, r2, r3, r4);
    root.getChildren().add(vbox);

    primaryStage.setScene(scene);
    primaryStage.show();
  }
  public static void main(String[] args) {
    launch(args);
  }
}

The code above generates the following result.

null




VBox with spacing

VBox vbox = new VBox(8); // spacing = 8
vbox.getChildren().addAll(new Button("Cut"), new Button("Copy"), new Button("Paste"));


import javafx.application.Application;<!--from  w  w  w  .  j  ava  2  s.  co  m-->
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
 
public class Main extends Application {

    @Override
    public void start(final Stage stage) {
        stage.setTitle("HTML");
        stage.setWidth(500);
        stage.setHeight(500);
        Scene scene = new Scene(new Group());
 
        VBox vbox = new VBox(8); // spacing = 8
        vbox.getChildren().addAll(new Button("Cut"), new Button("Copy"), new Button("Paste"));
        
        scene.setRoot(vbox);

        stage.setScene(scene);
        stage.show();
    }
 
    public static void main(String[] args) {
        launch(args);
    }
}

The code above generates the following result.

null

Set Padding and Spacing

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;
/*from   ww  w .ja v  a 2s .c o m*/
public class Main extends Application {
  public static void main(String[] args) {
    Application.launch(args);
  }

  @Override
  public void start(Stage primaryStage) {
    primaryStage.setTitle("VBox Test");

    // VBox
    VBox vb = new VBox();
    vb.setPadding(new Insets(10, 50, 50, 50));
    vb.setSpacing(10);

    Label lbl = new Label("VBox");
    lbl.setFont(Font.font("Amble CN", FontWeight.BOLD, 24));
    vb.getChildren().add(lbl);

    // Buttons
    Button btn1 = new Button();
    btn1.setText("Button1");
    vb.getChildren().add(btn1);

    Button btn2 = new Button();
    btn2.setText("Button2");
    vb.getChildren().add(btn2);

    Button btn3 = new Button();
    btn3.setText("Button3");
    vb.getChildren().add(btn3);

    Button btn4 = new Button();
    btn4.setText("Button4");
    vb.getChildren().add(btn4);

    // Adding VBox to the scene
    Scene scene = new Scene(vb);
    primaryStage.setScene(scene);
    primaryStage.show();
  }
}

The code above generates the following result.

null