BorderPane class is a part of JavaFX. BorderPane class lays its children in top, bottom, center, left and, right positions. BorderPane lays out each child set in the five positions regardless of the child’s visible property value unmanaged children are ignored. BorderPane class inherits Pane class.
Constructor of the class:
- BorderPane(): Creates a new Border Pane.
- BorderPane(Node c): Creates a new Border Pane with specified node at center.
- BorderPane(Node center, Node top, Node right, Node bottom, Node left): Creates an BorderPane layout with the given Nodes to use for each of the main layout areas of the Border Pane.
Commonly Used Methods:
Methods | Explanation |
---|---|
getAlignment(Node c) | Returns the alignment of the node. |
getBottom() | Returns the bottom node of the border pane. |
getCenter() | Returns the center node of the border pane. |
getLeft() | Returns the left node of the border pane. |
getRight() | Returns the right node of the border pane. |
getTop() | Returns the top node of the border pane. |
setAlignment(Node c, Pos v) | Sets the alignment of node c to pos v. |
setBottom(Node v) | Sets the bottom node of the border pane. |
setCenter(Node v) | Sets the center node of the border pane. |
setLeft(Node v) | Sets the left node of the border pane. |
setRight(Node v) | Sets the right node of the border pane. |
setTop(Node v) | Sets the top node of the border pane. |
Below programs illustrate the use of BorderPane Class:
-
Java Program to create a BorderPane and add it to the stage: In this program we create a Label named label. Now create a BorderPane named borderpane. We will add this label to the BorderPane layout in its center. Add the border pane to the scene and add this scene to the stage and display the stage to show the final results.
// Java Program to create a BorderPane // and add it to the stage import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.canvas.*;
import javafx.scene.web.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.shape.*;
public class BorderPane_1 extends Application {
// launch the application
public void start(Stage stage)
{
try {
// set title for the stage
stage.setTitle( "BorderPane" );
// create a label
Label label = new Label( "this is BorderPane example" );
// create a BorderPane
BorderPane border_pane = new BorderPane(label);
// create a scene
Scene scene = new Scene(border_pane, 400 , 300 );
// set the scene
stage.setScene(scene);
stage.show();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
// Main Method
public static void main(String args[])
{
// launch the application
launch(args);
}
} |
Output:
-
Java Program to create a BorderPane and add Center, top, bottom, left, right components and add it to the stage: In this program we create Labels named label_center, label_top, label_bottom, label_right, label_left. Now create a BorderPane named borderpane. We will add this label to the BorderPane layout in its center, top, bottom, right, left. Set the alignments of the labels to center by using setAlignment(). We will add border pane to the scene and add this scene to the stage and display the stage to show the final results.
// Java Program to create a BorderPane and // add Center, top, bottom, left, right // components and add it to the stage import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.canvas.*;
import javafx.scene.web.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.shape.*;
import javafx.geometry.*;
public class BorderPane_2 extends Application {
// launch the application
public void start(Stage stage)
{
try {
// set title for the stage
stage.setTitle( "BorderPane" );
// create a label
Label label_center = new Label( "this is BorderPane center" );
Label label_top = new Label( "this is BorderPane top" );
Label label_bottom = new Label( "this is BorderPane bottom" );
Label label_left = new Label( "this is BorderPane left" );
Label label_right = new Label( "this is BorderPane right" );
// create a BorderPane
BorderPane border_pane = new BorderPane(label_center,
label_top, label_right, label_bottom, label_left);
// set alignment
border_pane.setAlignment(label_top, Pos.CENTER);
border_pane.setAlignment(label_bottom, Pos.CENTER);
border_pane.setAlignment(label_left, Pos.CENTER);
border_pane.setAlignment(label_right, Pos.CENTER);
// create a scene
Scene scene = new Scene(border_pane, 400 , 300 );
// set the scene
stage.setScene(scene);
stage.show();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
// Main Method
public static void main(String args[])
{
// launch the application
launch(args);
}
} |
Output:
Note: The above programs might not run in an online IDE please use an offline compiler.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/BorderPane.html