JavaFX Tutorial Index

JavaFX Tutorial JavaFX Installation JavaFX Application Structure JavaFX Architecture

JavaFX Applications Charts

JavaFX Bar Chart JavaFX Bubble Chart JavaFX Pie Chart JavaFX Scatter Chart JavaFX Area Chart JavaFX Line Chart

JavaFX 2D Shapes

JavaFX Arc JavaFX Circle JavaFX Ellipse JavaFX Line JavaFX Polygon JavaFX Rectangle JavaFX Color JavaFX Gradient Color

JavaFX 3D Shapes

JavaFX Box JavaFX Cylinder JavaFX Sphere

JavaFX Animations

JavaFX Rotate Transition JavaFX Translate Transition JavaFX Fade Transition JavaFX Fill Transition JavaFX Parallel Transition JavaFX Path Transition JavaFX Pause Transition JavaFX Scale Transition JavaFX Sequential Transition JavaFX Stroke Transition

JavaFX CSS

JavaFX ID Selector JavaFX Inline Styles JavaFX Selectors

JavaFX Effect

JavaFX Blend JavaFX Bloom JavaFX Color Adjust JavaFX Color Input JavaFX Drop Shadow JavaFX Gaussian Blur JavaFX Glow JavaFX Image Input JavaFX Inner Shadow JavaFX Light Distant JavaFX Light Point JavaFX Light Spot JavaFX Lighting JavaFX Motion Blur JavaFX Reflection JavaFX Shadow

JavaFX Layouts

JavaFX Layouts JavaFX BorderPane JavaFX GridPane JavaFX StackPane JavaFX HBox JavaFX TilePane

JavaFX Event Handling

JavaFX Event Handling JavaFX Event Filters JavaFX Convenience Methods

JavaFX Transformation

JavaFX Transformation JavaFX Scaling JavaFX Rotation JavaFX Translation JavaFX Shear

JavaFX UI

JavaFX Menu JavaFX Button JavaFX Button Styling JavaFX CheckBox JavaFX File Chooser JavaFX HyperLink JavaFX Label JavaFX UI Controls JavaFX PasswordField JavaFX ProgressBar JavaFX RadioButton JavaFX ScrollBar JavaFX Slider JavaFX TextField

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