Basic Layout Panes in JavaFX

By Doug Lowe

Part of JavaFX For Dummies Cheat Sheet

Layout panes in JavaFX let you manage the placement controls and shapes that appear in your scenes. Don’t forget that a layout pane is itself a node, so you can nest layout panes within other layout panes to create complicated layouts.

HBox

HBox hbox1 = new HBox(10);     // sets spacing
hbox1.getChildren().addAll(btn1, btn2, btn3);
hbox1.setAlignment(Pos.CENTER);
hbox1.setPadding(new Insets(10));
hbox1.setSpacing(10);

VBox

VBox vbox1 = new VBox(10);     // sets spacing
vbox1.getChildren().addAll(btn1, btn2, btn3);
vbox1.setAlignment(Pos.CENTER);
vbox1.setPadding(new Insets(10));
vbox1.setSpacing(10);

FlowPane

FlowPane flow1 = new FlowPane(10, 10);
flow1.getChildren().addAll(btn1, btn2, btn3);
flow1.setAlignment(Pos.CENTER);
flow1.setOrientation(Orientation.VERTICAL);
flow1.setVgap(10);
flow1.setHgap(10);
flow1.setPrefWrapLength(5);

BorderPane

BorderPane border1 = new BorderPane();
border1.setCenter(btn1);
border1.setTop(btn2);
border1.setRight(btn3);
border1.setBottom(btn4);
border1.setLeft(btn5);