Wednesday 11 November 2020

JavaFX: StackPane

StackPane layout place UI components in a single stack. The z-order of the children is defined by the order of the children list with the 0th child being the bottom and last child on top.  If a border and/or padding have been set, the children will be layed out within those insets.

 

Example

Let’s overlay an ellipse on Rectangle using StackPane.

 

Find the below working application.

 

stackPaneDemo.fxml 

<?import javafx.scene.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<StackPane xmlns:fx="http://javafx.com/fxml" fx:controller="com.sample.app.layout.controller.StackPaneController"
 	fx:id="stackPane1">

	<Rectangle fx:id="rectangle1" 
		fill="lightsalmon"
		width="300" height="200"
		x="50" y = "50" 
	 />
	 
	 <Ellipse fx:id="circle1" 
	 	centerX="175" centerY="125"
		radiusX ="150" radiusY="100"
		fill="lightblue"
	/>
	

</StackPane>

 

StackPaneController.java

package com.sample.app.layout.controller;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;

public class StackPaneController implements Initializable {

	@FXML
	private StackPane stackPane1;
	

	@Override
	public void initialize(URL location, ResourceBundle resources) {
		DropShadow dropShadow = new DropShadow();
		dropShadow.setOffsetX(5);
		dropShadow.setOffsetY(5);
		dropShadow.setColor(Color.GRAY);
		

		stackPane1.setEffect(dropShadow);
		stackPane1.setAlignment(Pos.TOP_CENTER);
		stackPane1.setPadding(new Insets(20, 20, 20, 20));

	}

}

 

StackPaneDemo.java

package com.sample.app.layout;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class StackPaneDemo extends Application {

	private static final String FXML_FILE = "/stackPaneDemo.fxml";
	private static final String STAGE_TITLE = "StackPane Demo";

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

	}

	@Override
	public void start(Stage primaryStage) throws Exception {

		Parent root = (Parent) FXMLLoader.load(this.getClass().getResource(FXML_FILE));

		Scene scene = new Scene(root, 500, 400, Color.WHITE);

		primaryStage.setTitle(STAGE_TITLE);
		primaryStage.setScene(scene);
		primaryStage.show();
	}

}

 

package com.sample.app.layout;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class StackPaneDemo extends Application {

	private static final String FXML_FILE = "/stackPaneDemo.fxml";
	private static final String STAGE_TITLE = "StackPane Demo";

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

	}

	@Override
	public void start(Stage primaryStage) throws Exception {

		Parent root = (Parent) FXMLLoader.load(this.getClass().getResource(FXML_FILE));

		Scene scene = new Scene(root, 500, 400, Color.WHITE);

		primaryStage.setTitle(STAGE_TITLE);
		primaryStage.setScene(scene);
		primaryStage.show();
	}

}

 

Output

 


 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment