Layouts

HBoxes

// HBoxes.java
//    Demonstrates the use of an hbox layout.

import javafx.application.*;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;

public class HBoxes extends Application {

  public void start(Stage stage) {
    // Create some buttons.
    Button button1 = new Button("First Short Button");
    Button button2 = new Button("Long ... ... ... ... ... ... ... ... ... Button");
    Button button3 = new Button("Second Short Button");
    Button button4 = new Button("Third Short Button");

    // Create a pane to hold the buttons.
    HBox pane = new HBox(30);
    pane.setPadding(new Insets(30, 30, 30, 30));
    pane.getChildren().addAll(button1, button2, button3, button4);

    // Create a scene.
    Scene scene = new Scene(pane);

    // Set up the stage and make it visible.
    stage.setTitle("HBoxes");
    stage.setScene(scene);
    stage.show();
  }

  public static void main(String[] args) {
    launch(args);
  }
}

VBoxes

// VBoxes.java
//    Demonstrates the use of a vbox layout.

import javafx.application.*;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;

public class VBoxes extends Application {

  public void start(Stage stage) {
    // Create some buttons.
    Button button1 = new Button("First Short Button");
    Button button2 = new Button("Long ... ... ... ... ... ... ... ... ... Button");
    Button button3 = new Button("Second Short Button");
    Button button4 = new Button("Third Short Button");

    // Create a pane to hold the buttons.
    VBox pane = new VBox(30);
    pane.setPadding(new Insets(30, 30, 30, 30));
    pane.getChildren().addAll(button1, button2, button3, button4);

    // Create a scene.
    Scene scene = new Scene(pane);

    // Set up the stage and make it visible.
    stage.setTitle("VBoxes");
    stage.setScene(scene);
    stage.show();
  }

  public static void main(String[] args) {
    launch(args);
  }
}

FlowPanes

// FlowPanes.java
//    Demonstrates the use of a flowpane layout.

import javafx.application.*;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;

public class FlowPanes extends Application {

  public void start(Stage stage) {
    // Create some buttons.
    Button button1 = new Button("First Short Button");
    Button button2 = new Button("Long ... ... ... ... ... ... ... ... ... Button");
    Button button3 = new Button("Second Short Button");
    Button button4 = new Button("Third Short Button");

    // Create a pane to hold the buttons.
    FlowPane pane = new FlowPane(30, 30);
    pane.setPadding(new Insets(30, 30, 30, 30));
    pane.getChildren().addAll(button1, button2, button3, button4);

    // Create a scene.
    Scene scene = new Scene(pane);

    // Set up the stage and make it visible.
    stage.setTitle("FlowPanes");
    stage.setScene(scene);
    stage.show();
  }

  public static void main(String[] args) {
    launch(args);
  }
}

BorderPanes

// BorderPanes.java
//    Demonstrates the use of a borderpane layout.

import javafx.application.*;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;

public class BorderPanes extends Application {

  public void start(Stage stage) {
    // Create some buttons.
    Button button1 = new Button("First Short Button");
    Button button2 = new Button("Long ... ... ... ... ... ... ... ... ... Button");
    Button button3 = new Button("Second Short Button");
    Button button4 = new Button("Third Short Button");

    // Create a pane to hold the buttons.
    BorderPane pane = new BorderPane();
    pane.setPadding(new Insets(30, 30, 30, 30));
    pane.setCenter(button1);
    pane.setLeft(button2);
    pane.setRight(button3);
    pane.setTop(button4);
    

    // Create a scene.
    Scene scene = new Scene(pane);

    // Set up the stage and make it visible.
    stage.setTitle("BorderPanes");
    stage.setScene(scene);
    stage.show();
  }

  public static void main(String[] args) {
    launch(args);
  }
}

GridPanes

// GridPanes.java
//    Demonstrates the use of a gridpane layout.

import javafx.application.*;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;

public class GridPanes extends Application {

  public void start(Stage stage) {
    // Create some buttons.
    Button button1 = new Button("First Short Button");
    Button button2 = new Button("Long ... ... ... ... ... ... ... ... ... Button");
    Button button3 = new Button("Second Short Button");
    Button button4 = new Button("Third Short Button");

    // Create a pane to hold the buttons.
    GridPane pane = new GridPane();
    pane.setPadding(new Insets(30, 30, 30, 30));
    pane.setHgap(30);
    pane.setVgap(30);
    pane.add(button1, 0, 0);
    pane.add(button2, 0, 1);
    pane.add(button3, 1, 0);
    pane.add(button4, 1, 1);

    // Create a scene.
    Scene scene = new Scene(pane);

    // Set up the stage and make it visible.
    stage.setTitle("GridPanes");
    stage.setScene(scene);
    stage.show();
  }

  public static void main(String[] args) {
    launch(args);
  }
}

TilePanes

// TilePanes.java
//    Demonstrates the use of a tilepane layout.

import javafx.application.*;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;

public class TilePanes extends Application {

  public void start(Stage stage) {
    // Create some buttons.
    Button button1 = new Button("First Short Button");
    Button button2 = new Button("Long ... ... ... ... ... ... ... ... ... Button");
    Button button3 = new Button("Second Short Button");
    Button button4 = new Button("Third Short Button");

    // Create a pane to hold the buttons.
    TilePane pane = new TilePane(30, 30);
    pane.setPadding(new Insets(30, 30, 30, 30));
    pane.getChildren().addAll(button1, button2, button3, button4);

    // Create a scene.
    Scene scene = new Scene(pane);

    // Set up the stage and make it visible.
    stage.setTitle("TilePanes");
    stage.setScene(scene);
    stage.show();
  }

  public static void main(String[] args) {
    launch(args);
  }
}

ScrollPanes

// ScrollPanes.java
//    Demonstrates use of ScrollPanes

import javafx.application.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.image.*;
import javafx.scene.layout.*;
import javafx.stage.*;

public class ScrollPanes extends Application {

  public void start(Stage stage) {
    // Create a label.
    Label label = new Label("", new ImageView("tux.png"));

    // Create a pane to hold the label.
    ScrollPane pane = new ScrollPane();
    pane.setContent(label);

    // Create a scene.
    Scene scene = new Scene(pane, 300, 300);

    // Set up the stage and make it visible.
    stage.setTitle("ScrollPanes");
    stage.setScene(scene);
    stage.show();
  }

  public static void main(String[] args) {
    launch(args);
  }
}