JavaFX - Layout BorderPane

If we use the BorderPane, the nodes are arranged in the Top, Left, Right, Bottom and Center positions.
The class named BorderPane of the package javafx.scene.layout represents the BorderPane.
This class contains five properties, which include −
bottom − This property is of Node type and it represents the node placed at the bottom of the BorderPane. You can set value to this property using the setter method setBottom().
bottom − This property is of Node type and it represents the node placed at the bottom of the BorderPane. You can set value to this property using the setter method setBottom().
center − This property is of Node type and it represents the node placed at the center of the BorderPane. You can set value to this property using the setter method setCenter().
center − This property is of Node type and it represents the node placed at the center of the BorderPane. You can set value to this property using the setter method setCenter().
left − This property is of Node type and it represents the node placed at the left of the BorderPane. You can set value to this property using the setter method setLeft().
left − This property is of Node type and it represents the node placed at the left of the BorderPane. You can set value to this property using the setter method setLeft().
right − This property is of Node type and it represents the node placed at the right of the BorderPane. You can set value to this property using the setter method setRight().
right − This property is of Node type and it represents the node placed at the right of the BorderPane. You can set value to this property using the setter method setRight().
top − This property is of Node type and it represents the node placed at the top of the BorderPane. You can set value to this property using the setter method setTop().
top − This property is of Node type and it represents the node placed at the top of the BorderPane. You can set value to this property using the setter method setTop().
In addition to these, this class also provides the following method −
setAlignment() − This method is used to set the alignment of the nodes belonging to this pane. This method accepts a node and a priority value.
setAlignment() − This method is used to set the alignment of the nodes belonging to this pane. This method accepts a node and a priority value.
The following program is an example of the BorderPane layout. In this, we are inserting a five text fields in the Top, Bottom, Right, Left and Center positions.
Save this code in a file with the name BorderPaneExample.java.
import javafx.application.Application; 
import javafx.collections.ObservableList; 
import javafx.scene.Scene; 
import javafx.scene.control.TextField; 
import javafx.scene.layout.BorderPane; 
import javafx.stage.Stage; 
         
public class BorderPaneExample extends Application { 
   @Override 
   public void start(Stage stage) {      
      //Instantiating the BorderPane class  
      BorderPane bPane = new BorderPane();   
       
      //Setting the top, bottom, center, right and left nodes to the pane 
      bPane.setTop(new TextField("Top")); 
      bPane.setBottom(new TextField("Bottom")); 
      bPane.setLeft(new TextField("Left")); 
      bPane.setRight(new TextField("Right")); 
      bPane.setCenter(new TextField("Center")); 
      
      //Creating a scene object 
      Scene scene = new Scene(bPane);  
      
      //Setting title to the Stage
      stage.setTitle("BorderPane Example"); 
         
      //Adding scene to the stage 
      stage.setScene(scene);          
      
      //Displaying the contents of the stage 
      stage.show(); 
   } 
   public static void main(String args[]){ 
      launch(args); 
   } 
}
Compile and execute the saved java file from the command prompt using the following commands.
javac BorderPaneExample.java 
java BorderPaneExample

On executing, the above program generates a JavaFX window as shown below.

    33 Lectures 
     7.5 hours 

 Syed Raza

    64 Lectures 
     12.5 hours 

 Emenwa Global, Ejike IfeanyiChukwu

    20 Lectures 
     4 hours 

 Emenwa Global, Ejike IfeanyiChukwu
 Print
 Add Notes
 Bookmark this page