×

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

In the JavaFX application, in order to slide over a range of data having values, Slider is used. It provides a pane with a Slider to move over a range...

4 minutes read.

JavaFX Path Transition

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

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 BorderPane

In the JavaFX application, in order to set Border Pane as a layout, the BorderPane class is used. The BorderPane layout allows us to arrange the components in the left,...

4 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 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 Convenience Methods

The JavaFX allows us to use various convenience methods to handle events on various nodes in JavaFX applications. The convenience methods provides us easy and effective way to add any...

4 minutes read.

JavaFX Sphere

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

4 minutes read.

JavaFX UI Controls

We can make an advanced graphical user interface (GUI) for the JavaFX application using UI Controls and layouts. Various UI elements can be used b the programmer in their application...

3 minutes read.

JavaFX Image Input

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

4 minutes read.

JavaFX Event Filters

In JavaFX, in order to handle the event which are created by any of the mouse action, keyboard action, rotate action, scroll action Event filters are used. The event filters...

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

In the JavaFX application, in order to draw a line, the Line class is used. The line allows us to join any two points with x and y coordinates in...

3 minutes read.

JavaFX Circle

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

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 Shear

In the JavaFX application, in order to apply various shear effect, we use this class. It has the ability to shear the given object along the x or y axis....

3 minutes read.

JavaFX Stroke Transition

In the JavaFX application, in order to generate the stroke transition, we have to apply stroke transition on any particular shape. It has the ability to give different color strokes...

4 minutes read.