StackPane class is a part of JavaFX. StackPane class lays out its children in form of a stack. The new node is placed on the top of the previous node in a StackPane. StackPane class inherits Pane Class.
Constructors of the class:
- StackPane(): Creates a new empty StackPane.
- StackPane(Node… c): Creates a new StackPne with specified nodes.
Commonly Used Methods:
| Method | Explanation |
|---|---|
| getAlignment() | Returns the alignment of the StackPane. |
| getAlignment(Node c) | Returns the node’s alignment. |
| getMargin(Node c) | Returns the insets of the node. |
| setAlignment(Node n, Pos v) | Sets the alignment of the node which is a part of StackPane. |
| setAlignment(Pos v) | Sets the alignment of the StackPane. |
| setMargin(Node n, Insets v) | Sets the margin of the node which is a part of StackPane. |
Below programs illustrate the use of StackPane Class:
- Java Program to create a StackPane, add circle, label, rectangle and add it to the stage: In this program we are creating a Label named label, a Rectangle named rectangle and a Circle named circle. Then set the font of the StackPane using the setFont() function. Now set the fill of the rectangle and circle using the setFill() function. We will then create a StackPane named stack_pane and add rectangle, circle and label. Create a scene and add the stack_pane to the scene. Add this scene to the stage and call the show() function to display the final results.
// Java Program to create a StackPane,// add circle, label, rectangle// and add it to the stageimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.event.ActionEvent;importjavafx.scene.paint.*;importjavafx.scene.canvas.*;importjavafx.scene.text.*;importjavafx.scene.Group;importjavafx.scene.shape.*;publicclassStackPane_1extendsApplication {// launch the applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("StackPane");// create a labelLabel label =newLabel("this is StackPane example");// set Font for labellabel.setFont(newFont(30));// create a circleCircle circle =newCircle(100,100,70);// set fill for the circlecircle.setFill(Color.RED);// create RectangleRectangle rectangle =newRectangle(100,100,180,160);// set fill for rectanglerectangle.setFill(Color.BLUE);// create a stack paneStackPane stack_pane =newStackPane(rectangle, circle, label);// create a sceneScene scene =newScene(stack_pane,400,300);// set the scenestage.setScene(scene);stage.show();}catch(Exception e) {System.out.println(e.getMessage());}}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}Output:
- Java Program to create a StackPane, add the circle, label, rectangle and then set the alignment of the StackPane and add it to the stage: In this program we are creating a Label named label, a Rectangle named rectangle and a Circle named circle. Then set the font of the StackPane using the setFont() function. Set fill of the rectangle and circle using the setFill() function. Now create a StackPane named stack_pane and add rectangle, circle, and label. Set the alignment of the stack_pane using setAlignment() function. Create a scene and add the stack_pane to the scene. Finally add this scene to the stage and call the show() function to display the results.
// Java Program to create a StackPane,// add the circle, label, rectangle and// then set the alignment of the StackPane// and add it to the stageimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.geometry.*;importjavafx.scene.paint.*;importjavafx.scene.canvas.*;importjavafx.scene.text.*;importjavafx.scene.Group;importjavafx.scene.shape.*;publicclassStackPane_2extendsApplication {// launch the applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("StackPane");// create a labelLabel label =newLabel("this is StackPane example");// set Font for labellabel.setFont(newFont(30));// create a circleCircle circle =newCircle(100,100,70);// set fill for the circlecircle.setFill(Color.RED);// create RectangleRectangle rectangle =newRectangle(100,100,180,160);// set fill for rectanglerectangle.setFill(Color.BLUE);// create a stack paneStackPane stack_pane =newStackPane(rectangle, circle, label);// set alignement for the stack panestack_pane.setAlignment(Pos.TOP_CENTER);// create a sceneScene scene =newScene(stack_pane,400,300);// set the scenestage.setScene(scene);stage.show();}catch(Exception e) {System.out.println(e.getMessage());}}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}Output:
Note: The above programs might not run in an online IDE please use an offline compiler.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/StackPane.html

