JavaFX BorderPane Layout

0

BorderPane Layout in JavaFX:

  • The BorderPane layouts child nodes in a top, bottom, left, right, or centre region.
  • Top and bottom can take full available width and left and right can take full height.
  • Each region can only have one node.

Method :

setABC(node)

ABC =top, left, bottom, right, centre
These method is used to set node at top, left, bottom, right and centre position.

Example of Borderpane Layout :


package javafxtuts;


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

/**
 *
 * @author javafxtuts
 */
public class Javafxtuts extends Application {
    
    @Override
    
  public void start(Stage primaryStage) {
    primaryStage.setTitle("JavaFXtuts");
    BorderPane root = new BorderPane();
    
    
    
    Button btnTop = new Button("Top");
    btnTop.setPadding(new Insets(10, 150, 10, 100));
    root.setTop(btnTop);

    Button btnLeft = new Button("Left");
    btnLeft.setPadding(new Insets(80, 20, 90, 20));
    root.setLeft(btnLeft);

    Button btnCenter = new Button("Center");
    ///btnCenter.setPadding(new Insets(80, 20, 90, 20));
    root.setCenter(btnCenter);

    Button btnRight = new Button("Right");
    btnRight.setPadding(new Insets(80, 20, 90, 20));
    root.setRight(btnRight);

    Button btnBottom = new Button("Bottom");
    btnBottom.setPadding(new Insets(10, 150, 10, 100));
    root.setBottom(btnBottom);

    Scene scene = new Scene(root, 300, 300);
    primaryStage.setScene(scene);
    primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
}

Output:

Explain of borderPane layout example:

    • setPading() method set some space around the pane.
Button btnRight = new Button("Right");
    btnRight.setPadding(new Insets(80, 20, 90, 20));
    root.setRight(btnRight);
  • And setRight() method set node in right side of BorderPane.
Share.

Leave A Reply