Sunday, December 29, 2024
Google search engine
HomeLanguagesJavaJavaFX | StackPane Class

JavaFX | StackPane Class

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:

  1. StackPane(): Creates a new empty StackPane.
  2. 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:

  1. 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 stage
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    import javafx.event.ActionEvent;
    import javafx.scene.paint.*;
    import javafx.scene.canvas.*;
    import javafx.scene.text.*;
    import javafx.scene.Group;
    import javafx.scene.shape.*;
    Ā Ā 
    public class StackPane_1 extends Application {
    Ā Ā 
    Ā Ā Ā Ā // launch the application
    Ā Ā Ā Ā public void start(Stage stage)
    Ā Ā Ā Ā {
    Ā Ā 
    Ā Ā Ā Ā Ā Ā Ā Ā try {
    Ā Ā 
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // set title for the stage
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā stage.setTitle("StackPane");
    Ā Ā 
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // create a label
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Label label = new Label("this is StackPane example");
    Ā Ā 
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // set Font for label
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā label.setFont(new Font(30));
    Ā Ā 
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // create a circle
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Circle circle = new Circle(100, 100, 70);
    Ā Ā 
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // set fill forĀ  the circle
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā circle.setFill(Color.RED);
    Ā Ā 
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // create Rectangle
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Rectangle rectangle = new Rectangle(100, 100, 180, 160);
    Ā Ā 
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // set fill for rectangle
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā rectangle.setFill(Color.BLUE);
    Ā Ā 
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // create a stack pane
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā StackPane stack_pane = new StackPane(rectangle, circle, label);
    Ā Ā 
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // create a scene
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Scene scene = new Scene(stack_pane, 400, 300);
    Ā Ā 
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // set the scene
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā stage.setScene(scene);
    Ā Ā 
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā stage.show();
    Ā Ā Ā Ā Ā Ā Ā Ā }
    Ā Ā 
    Ā Ā Ā Ā Ā Ā Ā Ā catch (Exception e) {
    Ā Ā 
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(e.getMessage());
    Ā Ā Ā Ā Ā Ā Ā Ā }
    Ā Ā Ā Ā }
    Ā Ā 
    Ā Ā Ā Ā // Main Method
    Ā Ā Ā Ā public static void main(String args[])
    Ā Ā Ā Ā {
    Ā Ā 
    Ā Ā Ā Ā Ā Ā Ā Ā // launch the application
    Ā Ā Ā Ā Ā Ā Ā Ā launch(args);
    Ā Ā Ā Ā }
    }

    
    

    Output:

  2. 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 stage
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    import javafx.geometry.*;
    import javafx.scene.paint.*;
    import javafx.scene.canvas.*;
    import javafx.scene.text.*;
    import javafx.scene.Group;
    import javafx.scene.shape.*;
    Ā Ā 
    public class StackPane_2 extends Application {
    Ā Ā 
    Ā Ā Ā Ā // launch the application
    Ā Ā Ā Ā public void start(Stage stage)
    Ā Ā Ā Ā {
    Ā Ā 
    Ā Ā Ā Ā Ā Ā Ā Ā try {
    Ā Ā 
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // set title for the stage
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā stage.setTitle("StackPane");
    Ā Ā 
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // create a label
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Label label = new Label("this is StackPane example");
    Ā Ā 
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // set Font for label
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā label.setFont(new Font(30));
    Ā Ā 
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // create a circle
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Circle circle = new Circle(100, 100, 70);
    Ā Ā 
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // set fill forĀ  the circle
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā circle.setFill(Color.RED);
    Ā Ā 
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // create Rectangle
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Rectangle rectangle = new Rectangle(100, 100, 180, 160);
    Ā Ā 
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // set fill for rectangle
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā rectangle.setFill(Color.BLUE);
    Ā Ā 
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // create a stack pane
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā StackPane stack_pane = new StackPane(rectangle, circle, label);
    Ā Ā 
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // set alignement for the stack pane
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā stack_pane.setAlignment(Pos.TOP_CENTER);
    Ā Ā 
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // create a scene
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Scene scene = new Scene(stack_pane, 400, 300);
    Ā Ā 
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // set the scene
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā stage.setScene(scene);
    Ā Ā 
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā stage.show();
    Ā Ā Ā Ā Ā Ā Ā Ā }
    Ā Ā 
    Ā Ā Ā Ā Ā Ā Ā Ā catch (Exception e) {
    Ā Ā 
    Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(e.getMessage());
    Ā Ā Ā Ā Ā Ā Ā Ā }
    Ā Ā Ā Ā }
    Ā Ā 
    Ā Ā Ā Ā // Main Method
    Ā Ā Ā Ā public static void main(String args[])
    Ā Ā Ā Ā {
    Ā Ā 
    Ā Ā Ā Ā Ā Ā Ā Ā // launch the application
    Ā Ā Ā Ā Ā Ā Ā Ā launch(args);
    Ā Ā Ā Ā }
    }

    
    

    Output:

  3. 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

RELATED ARTICLES

Most Popular

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?