JavaFX 2.1 Layouts: javafx.scene.layout.VBox

The javafx.scene.layout.VBox class arranges it child nodes in a single vertical column.

Below is an simple example of the javafx.scene.layout.VBox class

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.control.Label;
import javafx.stage.Stage;

public class LayoutsVBox extends Application {
  
  @Override
  public void start(Stage primaryStage) {
    VBox root = new VBox();
    
    root.getChildren().addAll(new Label("Label 1"),
                              new Label("Label 2"),
                              new Label("Label 3"),
                              new Label("Label 4"),
                              new Label("Label 5"));
    
    Scene scene = new Scene(root, 150, 100);
    
    primaryStage.setTitle("VBox");
    primaryStage.setScene(scene);
    primaryStage.show();
  }
}

The VBox is defined on line 11. Lines 13 through 17 define and initialize five Label objects that are added to the VBox. On line 19 the VBox is added to Scene. Below is the output of the above program.

The VBox layout can be used as child node to other JavaFX layouts to create more complex interfaces than a single layout can provide. Below is an example using the VBox layout as a child node of BoarderPane layout. See the post JavaFX 2.1 Layouts: javafx.scene.layout.BorderPane for more information on the BoarderPlan layout.

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.scene.control.Label;
import javafx.stage.Stage;

public class LayoutsVBox extends Application {
  
  @Override
  public void start(Stage primaryStage) {
    BorderPane root = new BorderPane();
    VBox right = new VBox();
    VBox left = new VBox();
    
    right.getChildren().addAll(new Label("Label 1"),
                               new Label("Label 2"),
                               new Label("Label 3"),
                               new Label("Label 4"),
                               new Label("Label 5"));
    
    left.getChildren().addAll(new Label("Label 1"),
                              new Label("Label 2"),
                              new Label("Label 3"),
                              new Label("Label 4"),
                              new Label("Label 5"));
    
    Label topLbl = new Label("Top");
    Label botLbl = new Label("Bottom");   
    Label cLbl   = new Label("Center");
     
    root.setTop(topLbl);
    root.setAlignment(topLbl, Pos.CENTER);
    root.setBottom(botLbl);
    root.setAlignment(botLbl, Pos.CENTER);
    root.setRight(right);
    root.setAlignment(right, Pos.CENTER);
    root.setCenter(cLbl);
    root.setLeft(left);
    root.setAlignment(left, Pos.CENTER);
    
    Scene scene = new Scene(root, 200, 200);
    
    primaryStage.setTitle("VBox");
    primaryStage.setScene(scene);
    primaryStage.show();
  }
}

Lines 13 through 15 define the BorderPane and two VBox objects. On lines 17 through 28 the five Label are defined and added to each VBox. The right VBox object is added to the BorderPane on line 37 and the left VBox object is added on line 40.

Below is the output of the second program demonstrating the VBox layout.

1 thought on “JavaFX 2.1 Layouts: javafx.scene.layout.VBox”

Leave a Reply

Your email address will not be published. Required fields are marked *