Updated March 24, 2023
Introduction to JavaFX BorderPane
JavaFX BorderPane is a container that lays out its children in different positions such as the bottom, top, left, right and center. Regardless of the visible property value of the child and unmanaged children, this pane lays the child in different positions. Java BorderPane can be instantiated from the class javafx.scene.layout.BorderPane. Since it satisfies all the requirements on the layout for a Graphical User Interface screen, this pane is commonly used in the scene’s root node. Now, let us see the syntax of BorderPane.
Syntax of JavaFX BorderPane
The syntax of BorderPane is as shown below.
BorderPane bp = new BorderPane();
Constructors of JavaFX BorderPane
The following are the constructors for JavaFX BorderPane.
- BorderPane() : A BorderPane layout will be created.
- BorderPane(Nodec): A BorderPane layout will be created with the specified node c as the center of the pane.
- BorderPane(Nodec, Node t, Node r, Node b, Node l): A BorderPane layout will be created with the specified node c as the center, t as the top, r as right, b as bottom and l as left of the pane.
Properties of JavaFX BorderPane
Java BorderPane has several properties, they are :
- bottom: On the bottom edge of the pane, the node will be placed.
- top: On the top edge of the pane, the node will be placed.
- right: On the right edge of the pane, the node will be placed.
- left: On the left edge of the pane, the node will be placed.
- center: On the center of the pane, the node will be placed.
Methods of JavaFX BorderPane
Java BorderPane has several methods that perform different functionalities. Some of the commonly used methods are explained below:
- clearConstraints(Nodechild): BorderPane constraints will be removed from the child node.
- computeMinWidth(double height): The minimum width of the region will be computed using this method.
- computeMinHeight(double width): The minimum height of the region will be computed using this method.
- getAlignment(Node c): Child alignment value will be returned.
- setAlignment(Node c, Posvalue): Child alignment value will be set.
- getMargin(Nodec): margin constraints value will be returned.
- setMargin(Nodec, Pos value): margin constraints value will be returned.
- computePrefWidth(double height): The preferred width for the region that is needed for the given height will be computed.
- computePrefHeight(double width): The preferred height for the region that is needed for the given width will be computed.
- getBottom(): The bottom property’s value will be returned.
- getCenter(): The center property’s value will be returned.
- getLeft(): The left property’s value will be returned.
- getRight(): The right property’s value will be returned.
- getTop(): The top property’s value will be returned.
- setBottom(Node v): The bottom property’s value v will be set.
- setCenter(Node v): Center property’s value v will be set.
- setLeft(Node v): The left property’s value v will be set.
- setRight(Node v): The right property value v will be set.
- setTop(Node v): The top property’s value v will be set.
Program to implement JavaFX BorderPane
Now, let us see different JavaFX programs to implement JavaFX BorderPane.
Program #1
Java program to create a border pane
Code:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.scene.control.*;
public class BorderPaneExample extends Application {
// application starts at this point
public void start(Stage s)
{
//try block
try {
// set title as BorderPane Example
s.setTitle("BorderPane Example");
// label creation
Label lb = new Label("This is a sample for BorderPane ");
// BorderPane creation
BorderPane bp = new BorderPane(lb);
// scene creation
Scene sc = new Scene(bp, 450, 350);
// set scene
s.setScene(sc);
//display the result
s.show();
}
//exception handling
catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
// Main Method
public static void main(String args[])
{
// launch the application
launch(args);
}
}
Output :
A border pane will be created with the title ‘BorderPane Example’ on executing the code.
Program #2
Java program to create a border pane with buttons on 5 different positions
Code:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.scene.control.*;
public class BorderPaneExample extends Application {
//application starts at this point
@Override
public void start(Stage s) throws Exception
{
//create borderpane
BorderPane bp = new BorderPane();
//set padding
bp.setPadding(new Insets(25, 30, 20, 20));
// Button on top
Button bt = new Button("Top Button");
//set padding
bt.setPadding(new Insets(20, 20, 20, 20));
//node on top
bp.setTop(bt);
//set margin
BorderPane.setMargin(bt, new Insets(20, 20, 20, 20));
// Button on left
Button bl = new Button("Left Button");
//set padding
bl.setPadding(new Insets(10, 10, 10, 10));
//node on left
bp.setLeft(bl);
// Set margin
BorderPane.setMargin(bl, new Insets(20, 20, 20, 20));
// Button on center
Button bc = new Button("Center Button");
//set padding
bc.setPadding(new Insets(10, 10, 10, 10));
//node on center
bp.setCenter(bc);
// set Alignment
BorderPane.setAlignment(bc, Pos.BOTTOM_CENTER);
// Button on right
Button br = new Button("Right Button");
//set padding
br.setPadding(new Insets(10, 10, 10, 10));
//node on right
bp.setRight(br);
// Set margin
BorderPane.setMargin(br, new Insets(20, 20, 20, 20));
// Button on bottom
Button bb = new Button("Bottom Button");
//set padding
bb.setPadding(new Insets(10, 10, 10, 10));
//noe on bottom
bp.setBottom(bb);
// set alignment.
BorderPane.setAlignment(bb, Pos.TOP_RIGHT);
// Set margin
BorderPane.setMargin(bb, new Insets(20, 20, 20, 20));
//scene creation
Scene sc = new Scene(bp, 650, 350);
//set title for the stage
s.setTitle("BorderPane Sample");
//set scene
s.setScene(sc);
//display result
s.show();
}
//main method
public static void main(String[] args) {
//launch the application
launch(args);
}
}
Output :
A border pane will be created with the title ‘BorderPane Sample ‘ and 5 buttons on executing the code. Moreover, these 5 buttons will be in 5 different positions such as top, left, right, bottom and center of the border pane as shown in the below figure.
Conclusion
In JavaFX, BorderPane lays out the children in five different positions. The positions are top, right, left, center and bottom. To use this pane, instantiate from the class javafx.scene.layout.BorderPane. More details such as syntax, constructor, properties, methods on Border Pane is discussed in this document in detail.
Recommended Articles
This is a guide to JavaFX BorderPane. Here we discuss the different JavaFX programs to implement JavaFX BorderPane along with the methods and properties. You may also have a look at the following articles to learn more –