mydomain
No ADS
No ADS

JavaFX BorderPane Layout Tutorial with Examples

  1. BorderPane Layout
  2. BorderPane example
  3. Design BorderPanel with Scene Builder

1. BorderPane Layout

No ADS
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, by default it will not fill the area up.
But if VBox or Hbox lies in an area of BorderPane, by default it will fill the area up.

2. BorderPane example

No ADS
BorderPaneDemo.java
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 {

  @Override
  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));
      root.setTop(btnTop);
      // 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));
      root.setLeft(btnLeft);
      // 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));
      root.setCenter(btnCenter);
       // Alignment.
       BorderPane.setAlignment(btnCenter, Pos.BOTTOM_CENTER);

      // RIGHT
      Button btnRight = new Button("Right");
      btnRight.setPadding(new Insets(5, 5, 5, 5));
      root.setRight(btnRight);
      // 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));
      root.setBottom(btnBottom);
      // 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");
      primaryStage.setScene(scene);
      primaryStage.show();
  }

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

}
Running the example:

3. Design BorderPanel with Scene Builder

No ADS
  • File/New/Other...
Add components to BorderPane:
Margin:
With BorderPane, you can set up Margin for subcomponents
Alignment
No ADS

JavaFX Tutorials

Show More
No ADS