×

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 Bloom

In the JavaFX application, in order to apply various bloom effect, we use the Bloom class. It has the ability to bloom the colors of the shape. All methods needed...

4 minutes read.

JavaFX Scatter Chart

In the JavaFX application, in order to create Scatter Chart, the ScatterChart class is used. The Scatter Chart allows us to plot data along the x and y axis of...

4 minutes read.

JavaFX Fade Transition

In the JavaFX application, in order to generate the fade transition, we have to apply fade transition on any particular shape. It has the ability to show the fading colors...

4 minutes read.

JavaFX Shadow

In the JavaFX application, in order to apply various Shadow effect from a single source, we use this class. It has the ability to create the shadow of the given...

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

In the JavaFX application, in order to apply various Scaling effects, we use this class. It has the ability to change the size of the given object. All methods needed...

5 minutes read.

JavaFX Bar Chart

In the JavaFX application, in order to create Bar Chart, the BarChart class is used. The Bar Chart allows us to plot data along different bars to show the scales...

4 minutes read.

JavaFX TilePane

In the JavaFX application, in order to set TilePane as a layout, the TilePane class is used. The TilePane layout allows us to arrange the components in the same sized...

4 minutes read.

JavaFX Motion Blur

In the JavaFX application, in order to apply various Motion blur effect, we use Motion Blur class. It has the ability to apply the Motion blur effect on the given...

4 minutes read.

JavaFX HyperLink

In the JavaFX application, in order to access and use webpages, hyperlinks are used. In HTML anchor tags are used in the same way in JavaFX hyperlinks are used. To...

4 minutes read.

JavaFX Button Styling

 In the JavaFX application, we can apply CSS styling to buttons in various ways. We can change the size of the button as well as border colour, text colour can...

3 minutes read.

JavaFX Tutorial

Introduction of JavaFX: The most popular applications called Rich Internet Application are build using the JavaFX. This application can run across many platforms. Various devices such as mobile, computers, and tablets...

4 minutes read.

JavaFX Inline Styles

In the JavaFX application, in order to apply various Inline css effect we will use the various css classes available in the JavaFX. It is used to apply the various...

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 Lighting

In the JavaFX application, in order to apply various Lighting effect from a single source, we use this class. It has the ability to lighten a node from a given...

4 minutes read.

JavaFX Color Input

In the JavaFX application, in order to apply various color input effect, we use the ColorInput class. It has the ability to fill the specified shape with a given color...

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 Box

In the JavaFX application, in order to draw a Box, the Box class is used. The Box allows us to form 3D shape having rectangular faces. All methods needed for...

4 minutes read.