×

JavaFX StackPane

In the JavaFX application, in order to set Stack Pane as a layout, the StackPane class is used. The StackPane layout allows us to arrange the components in the stack in which each component is placed on top of another component. All methods needed for this purpose are present in the javafx.scene.layout.StackPane class.

The constructor of StackPane layout:

1. StackPane():

It is used to create an empty constructor for the stack pane.

2. StackPane( Node newnode ):

This constructor is used to create a constructor with node specified.

Methods to set StackPane as a layout:

1. setAlignment(Node n, Pos v)

This property of the stack pane class allows us to define the alignment of the component at the pos of the container.

2. getAlignment(Node c)

This property of the stack pane class allows us to get the alignment of the specified node in the container.

3. getAlignment()

This property of the stack pane class allows us to get the alignment of the stack pane.

4. setMargin(Node n, Insets v)

This property of the Stack Pane class allows us to define the margin of the specified node on the container.

5. getMargin(Node c)

This property of the stack pane class is used to get margin property of the specified node.

Layouts in JavaFX applications- Simple StackPane

Example:

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


public class StackPaneUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
      
    Label newlabel = new Label(" StackPane example ");
    StackPane pane = new StackPane(newlabel);
      
        pane.setAlignment(Pos.TOP_CENTER);
         Scene scene = new Scene(pane,600,400);  
         
           primaryStage.setScene(scene);  
           primaryStage.setTitle(" JavaFX Layout -  StackPane layout Example");  
           primaryStage.show();  
        }


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

Output:

In order to create the Stack Pane in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.stage.Stage, javafx.scene.Scene, javafx.scene.control.Label, javafx.scene.layout.StackPane.

Then we have created one class named StackPaneUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold Stack Pane with the label, a Stack Pane object is created which is then passed to the Scene class object.

Stack Pane is created with the help of its constructor and component is passed in it.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is invoked. In output Frame like container is displayed with the title, " JavaFX Layout - Simple Stack Pane layout”. Also, it displays Stack Pane with the label of StackPane example placed in the top center.

Layouts in JavaFX applications- StackPane

Layouts in JavaFX applications- StackPane with UI components:

Example:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;


public class StackPaneUI extends Application 
{  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
      
        Circle circle = new Circle(100, 100, 70);
      
        circle.setFill(Color.BROWN);


        Rectangle rectangle = new Rectangle(100, 100, 180, 160);




        rectangle.setFill(Color.CYAN);


        StackPane pane = new StackPane(rectangle, circle);
      
         Scene scene = new Scene(pane,600,400);  
         
           primaryStage.setScene(scene);  


           primaryStage.setTitle(" JavaFX Layout -  StackPane layout Example");  


           primaryStage.show();  
        }


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

Output:

In order to create the Stack Pane with the UI components in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.stage.Stage, javafx.scene.Scene, javafx.scene.control.Label, javafx.scene.layout.StackPane, javafx.scene.shape.Rectangle, javafx.scene.shape.Circle.

Then we have created one class named StackPaneUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold Stack Pane with the UI components, a Stack Pane object is created which is then passed to the Scene class object.

Stack Pane is created with the help of its constructor and components are passed in it.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is invoked. In output Frame like container is displayed with the title, " JavaFX Layout -  Stack Pane layout Example ”. Also, it displays Stack Pane with Cyan colored rectangle and a red-colored circle on it.

Layouts in JavaFX applications- StackPane

Related Topics

JavaFX Gaussian Blur

In the JavaFX application, in order to apply various Gaussian blur effect, we use GaussianBlur class. It has the ability to apply blur effect on the given object. All methods...

4 minutes read.

JavaFX Arc

In the JavaFX application, in order to draw an arc, the Arc class is used. The Arc allows us to join any points in the space. All methods needed for...

5 minutes read.

JavaFX Scale Transition

In the JavaFX application, in order to generate the scale transition, we have to apply scale transition on any particular shape. It has the ability to scale the shape along...

4 minutes read.

JavaFX Selectors

In the JavaFX application, in order to apply various css effect, we use the various css classes available in the JavaFX. This is generally used to enhance the appearance property...

4 minutes read.

JavaFX Gradient Color

In the JavaFX application, in order to draw any shape, we are using classes present in the javafx.scene.shape package. We can fill the gradient colors in these shapes using the...

7 minutes read.

JavaFX Fill Transition

In the JavaFX application, in order to generate the fill transition, we have to apply fill transition on any particular shape. It has the ability to fill the given shape...

4 minutes read.

JavaFX Polygon

In the JavaFX application, in order to draw a polygon, the Polygon class is used. The Polygon allows us to join any number of points in the space. All methods...

4 minutes read.

JavaFX Menu

In the JavaFX application, in order to create a menu, menu items, and menu bar, Menu, MenuItem, and MenuBar class is used. The menu allows us to choose options among...

4 minutes read.

JavaFX TextField

In the JavaFX application, to get the user information as input from the user, a form is created using textfields which allows the user to enter input for specific fields....

4 minutes read.

JavaFX Color

In the JavaFX application, in order to draw any shape, we are using classes present in the javafx.scene.shape package. We can fill the colors in these shapes using the setFill()...

6 minutes read.

JavaFX Color Adjust

In the JavaFX application, in order to apply various color adjust effect, we use ColorAdjust class. It has the ability to apply various color adjust effect such as hue, saturation,...

5 minutes read.

JavaFX RadioButton

In the JavaFX application, to get the user information in the form of choice from the user, a form is created using radio buttons that allow the user to select...

3 minutes read.

JavaFX ID selector

In the JavaFX application, in order to apply various css effect using the id selector, we use the various css classes available in the JavaFX. This is generally used to...

4 minutes read.

JavaFX Light Distant

In the JavaFX application, in order to apply various Light Distant effect from a single source, we use this class. It has the ability to apply light from a distant...

4 minutes read.

JavaFX Inner Shadow

In the JavaFX application, in order to apply various Inner Shadow effect, we use this class. It has the ability to create the inner shadow of the given object. This...

4 minutes read.

JavaFX ScrollBar

In the JavaFX application, in order to scroll over a range of values, ScrollBar is used. It is used to view long page contents. In order to use ScrollBar in...

4 minutes read.

JavaFX Translate Transition

In the JavaFX application, in order to generate the translate transition, we have to apply translate transition on any particular shape. It has the ability to translate the shape along...

3 minutes read.

JavaFX Rotate Transition

In the JavaFX application, in order to generate the rotate transition, we have to apply rotation transition on any particular shape. It has the ability to rotate the shape along...

4 minutes read.

JavaFX Sequential Transition

In the JavaFX application, in order to generate the sequential transition, we have to apply sequential transition on any particular shape. It has the ability to execute multiple transactions sequentially...

4 minutes read.

JavaFX GridPane

In the JavaFX application, in order to set Grid Pane as a layout, the GridPane class is used. The GridPane layout allows us to arrange the components in the form...

4 minutes read.