JavaFX StackPane Layout


StackPane Layout In JavaFX :

  • JavaFX StackPane Layout put nodes in a stack.
  • Means StackPane layout places all of the nodes within a single stack.
  • In which each new node added on top of the previous node.

Example of StackPane Layout :

package javafxtuts;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

 * @author javafxtuts
public class Javafxtuts extends Application {
  public void start(Stage primaryStage) {
      double width = 400;
      double height = 300;
      StackPane root = new StackPane();
     Label lbl = new Label("JavaFX 8 StackPane");
    Button btn = new Button("Button");
      Scene scene = new Scene(root, width, height);

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

Output :

Explaination of StackPane example:

    • TO use StackPane as layout we create a object AS

StackPane root = new StackPane();

    • Now we add label after that we add a button which goes to top of the last node(lbl)
  • In StackPane by default alignment is centre so they appear in centre.

Leave A Reply