Wednesday, 11 November 2020

JavaFX: Anchor Pane

AnchorPane allows the edges of child nodes to be anchored to an offset from the anchor pane's edges.  If the anchor pane has a border and/or padding set, the offsets will be measured from the inside edge of those insets.

 

AnchroPane class provides following constructors to define AnchorPane instance.

 

public AnchorPane()

public AnchorPane(Node... children)

children: Specifies the initial set of children for this pane.

 

Following static methods are used to sets the anchor for the child.

 

public static void setTopAnchor(Node child, Double value)

Sets the top anchor for the child when contained by an anchor pane. If set, the anchor pane will maintain the child's size and position so that it's top is always offset by that amount from the anchor pane's top content edge.

 

public static void setLeftAnchor(Node child, Double value)

Sets the left anchor for the child when contained by an anchor pane. If set, the anchor pane will maintain the child's size and position so that it's left is always offset by that amount from the anchor pane's left content edge.

 

public static void setBottomAnchor(Node child, Double value)

Sets the bottom anchor for the child when contained by an anchor pane. If set, the anchor pane will maintain the child's size and position so that it's bottom is always offset by that amount from the anchor pane's bottom content edge.

 

public static void setRightAnchor(Node child, Double value)

Sets the right anchor for the child when contained by an anchor pane. If set, the anchor pane will maintain the child's size and position so that it's right is always offset by that amount from the anchor pane's right content edge.

 

Find the below working application.

 

AnchorPaneDemo.java 

package com.sample.app.layout;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class AnchorPaneDemo extends Application {

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

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

		AnchorPane anchorPane = new AnchorPane();

		Rectangle rect1 = new Rectangle(100, 100, Color.LIGHTGREEN);
		rect1.setArcHeight(15);
		rect1.setArcWidth(15);
		AnchorPane.setTopAnchor(rect1, 15d);
		AnchorPane.setLeftAnchor(rect1, 15d);

		Rectangle rect2 = new Rectangle(100, 100, Color.LIGHTCORAL);
		rect2.setArcHeight(15);
		rect2.setArcWidth(15);
		AnchorPane.setTopAnchor(rect2, 15d);
		AnchorPane.setRightAnchor(rect2, 15d);

		Rectangle rect3 = new Rectangle(100, 100, Color.LIGHTGREY);
		rect3.setArcHeight(15);
		rect3.setArcWidth(15);
		AnchorPane.setBottomAnchor(rect3, 15d);
		AnchorPane.setLeftAnchor(rect3, 15d);

		Rectangle rect4 = new Rectangle(100, 100, Color.LIGHTPINK);
		rect4.setArcHeight(15);
		rect4.setArcWidth(15);
		AnchorPane.setBottomAnchor(rect4, 15d);
		AnchorPane.setRightAnchor(rect4, 15d);

		Rectangle rect5 = new Rectangle(100, 100, Color.RED);
		rect5.setArcHeight(15);
		rect5.setArcWidth(15);
		AnchorPane.setBottomAnchor(rect5, 150d);
		AnchorPane.setLeftAnchor(rect5, 200d);

		anchorPane.getChildren().addAll(rect1, rect2, rect3, rect4, rect5);

		Scene scene = new Scene(anchorPane, 500, 400, Color.LIGHTYELLOW);

		primaryStage.setTitle("Anchor Pane Demo");
		primaryStage.setScene(scene);
		primaryStage.show();
	}

}

 

Output

 


 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment