JavaFX StackPane Layout

0

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) {
      primaryStage.setTitle("javafxtuts");
      double width = 400;
      double height = 300;
      StackPane root = new StackPane();
     Label lbl = new Label("JavaFX 8 StackPane");
    root.getChildren().add(lbl);
    Button btn = new Button("Button");
    root.getChildren().add(btn);
      
      Scene scene = new Scene(root, width, height);
      primaryStage.setScene(scene);
      primaryStage.show();
  }
  

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(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.
Share.

Leave A Reply