JavaFX BorderPane Layout Tutorial

1- BorderPane Layout

BorderPane is a container, which is divided into 5 separate areas, each of area can contain a subcomponent.
  • Top/Bottom area: Can shrink/expand horizontally and keep the height unchanged.
  • Left/Right area: Can shrink/expand vertically and keep the length unchanged.
  • Center area: Can shrink/expand in both directions.

Features of these above areas are illustrated as below:
If a certain area does not contain subcomponents, the area will be occupied by other areas.
For example: The area of TOP has no subcomponents, its space will be occupied by other components:
For example: The area of TOP & RIGHT has no subcomponents, its space will be occupied by other components:
Note: For JavaFX, the subcomponents lying in a certain area of BorderPane maybe not fill the space up, for example if Button lies in an area of BorderPane, it will not fill the area up.
But if VBox or Hbox lies in an area of BorderPane, it will fill the area up.

2- BorderPane example
package org.o7planning.javafx.borderpane;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class BorderPaneDemo extends Application {

  public void start(Stage primaryStage) throws Exception {
      BorderPane root = new BorderPane();

      root.setPadding(new Insets(15, 20, 10, 10));

      // TOP
      Button btnTop = new Button("Top");
      btnTop.setPadding(new Insets(10, 10, 10, 10));
      // Set margin for top area.
      BorderPane.setMargin(btnTop, new Insets(10, 10, 10, 10));

      // LEFT
      Button btnLeft = new Button("Left");
      btnLeft.setPadding(new Insets(5, 5, 5, 5));
      // Set margin for left area.
      BorderPane.setMargin(btnLeft, new Insets(10, 10, 10, 10));

      // CENTER
      Button btnCenter = new Button("Center");
      btnCenter.setPadding(new Insets(5, 5, 5, 5));
       // Alignment.
       BorderPane.setAlignment(btnCenter, Pos.BOTTOM_CENTER);

      // RIGHT
      Button btnRight = new Button("Right");
      btnRight.setPadding(new Insets(5, 5, 5, 5));
      // Set margin for right area.
      BorderPane.setMargin(btnRight, new Insets(10, 10, 10, 10));

      // BOTTOM
      Button btnBottom = new Button("Bottom");
      btnBottom.setPadding(new Insets(5, 5, 5, 5));
      // Alignment.
      BorderPane.setAlignment(btnBottom, Pos.TOP_RIGHT);

      // Set margin for bottom area.
      BorderPane.setMargin(btnBottom, new Insets(10, 10, 10, 10));

      Scene scene = new Scene(root, 550, 250);

      primaryStage.setTitle("BorderPane Layout Demo");

  public static void main(String[] args) {

Running the example:

3- Design BorderPanel with Scene Builder

  • File/New/Other...
Add components to BorderPane:


With BorderPane, you can set up Margin for subcomponents