×

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 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 - Basic Structure of Application

Every JavaFX application is mainly divided into three main components namely Scene, Stage, and nodes. For using these components we need to import javafx.application.Application class in every javafx application. Life...

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 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 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 Light Point

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

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 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 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 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 Cylinder

In the JavaFX application, in order to draw a Cylinder, the Cylinder class is used. The Cylinder allows us to form a 3D shape having rectangular faces and a straight...

4 minutes read.

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 Bubble Chart

In the JavaFX application, in order to create Bubble Chart, the BubbleChart class is used. The Bubble Chart allows us to plot three dimensional data. All methods needed for this...

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 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 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 Button

The button is one of the UI control present in the JavaFX. The javafx.scene.control.Button class is used for creating a button in javafx example. It is also possible to create...

5 minutes read.

JavaFX Label

The UI Control Label in a JavaFX is used to display the simple text. To use Label in JavaFX application javafx.scene.control.The label class is used. We can place Labels on...

3 minutes read.

JavaFX File Chooser

In the JavaFX application, in order to open or save the file, FileChooser is used. It allows user to choose the file from system location and save it in a...

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.