JavaFX 2 BorderPane Layout

This example demonstrates how to use a BorderPane in Java FX 2.1. The layout is composed of VBox containers which hold Label elements. Elements are styled using inline CSS.

BorderPaneExample.java

Click to View Source
   1 package com.example;
   2 
   3 import javafx.application.Application;
   4 import javafx.event.ActionEvent;
   5 import javafx.event.EventHandler;
   6 import javafx.geometry.Pos;
   7 import javafx.scene.Scene;
   8 import javafx.scene.control.Button;
   9 import javafx.scene.control.Label;
  10 import javafx.scene.layout.BorderPane;
  11 import javafx.scene.layout.VBox;
  12 import javafx.scene.text.Font;
  13 import javafx.scene.text.FontWeight;
  14 import javafx.stage.Stage;
  15 
  16 public class BorderPaneExample extends Application {
  17 
  18   BorderPane borderPane = new BorderPane(); // Container for the app
  19   
  20   // Setup UI elements here
  21   Label topLbl = new Label("Top");
  22   Label leftLbl = new Label("Left");
  23   Label rightLbl = new Label("Right");
  24   Label bottomLbl = new Label("Bottom");
  25   Button centerBtn = new Button("Center");
  26   
  27   // Using a VBox (Veritcal Box) to hold UI elements
  28   VBox topVb = new VBox();
  29   VBox leftVb = new VBox();
  30   VBox rightVb = new VBox();
  31   VBox bottomVb = new VBox();
  32   
  33   @Override
  34   public void init(){ // Use the init method to configure widgets
  35     // Set fonts for all labels using CSS
  36     topLbl.setFont(Font.font("Verdana", FontWeight.BOLD, 18));
  37     leftLbl.setFont(Font.font("Verdana", FontWeight.BOLD, 18));
  38     rightLbl.setFont(Font.font("Verdana", FontWeight.BOLD, 18));
  39     bottomLbl.setFont(Font.font("Verdana", FontWeight.BOLD, 18));
  40     
  41     // Configure the VBoxes
  42     topVb.getChildren().add(topLbl);
  43     topVb.setAlignment(Pos.CENTER);
  44     topVb.setStyle("-fx-border-stylel:solid; -fx-border-width:1pt; -fx-border-color:black;");
  45     
  46     leftVb.getChildren().add(leftLbl);
  47     leftVb.setAlignment(Pos.CENTER);
  48     leftVb.setStyle("-fx-border-stylel:solid; -fx-border-width:1pt; -fx-border-color:black;");    
  49     
  50     rightVb.getChildren().add(rightLbl);
  51     rightVb.setAlignment(Pos.CENTER);
  52     rightVb.setStyle("-fx-border-stylel:solid; -fx-border-width:1pt; -fx-border-color:black;");    
  53 
  54     bottomVb.getChildren().add(bottomLbl);
  55     bottomVb.setAlignment(Pos.CENTER);
  56     bottomVb.setStyle("-fx-border-stylel:solid; -fx-border-width:1pt; -fx-border-color:black;");  
  57     
  58     // Add VBoxes to Pane
  59     borderPane.setTop(topVb);
  60     borderPane.setLeft(leftVb);
  61     borderPane.setRight(rightVb);
  62     borderPane.setBottom(bottomVb);
  63     borderPane.setCenter(centerBtn);
  64  
  65     // Button event handler
  66     centerBtn.setOnAction(new EventHandler<ActionEvent>() {
  67 
  68       @Override
  69       public void handle(ActionEvent event) {
  70         System.out.println("Center clicked!");
  71       }
  72     });
  73     
  74   }
  75   
  76   public static void main(String[] args) {
  77     launch(args);
  78   }
  79   
  80   @Override
  81   public void start(Stage primaryStage) {
  82     primaryStage.setTitle("Border Pane Example");
  83 
  84     primaryStage.setScene(new Scene(borderPane, 300, 250));
  85     primaryStage.show();
  86   }
  87 }

Click to get NetBeans project