JavaFX - Layout Anchorpane


The Anchor Pane 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.

If we use an Anchor pane in our application, the nodes in it are anchored at a particular distance from the pane.

The class named AnchorPane of the package javafx.scene.layout represents the Anchor Pane. After a node is added, you need to set an anchor to it from the bounds of the pane in all directions (Top, Bottom, Right and Left). To set the anchor, this class provides four methods, which are – setBottomAnchor(), setTopAnchor(), setLeftAnchor(), setRightAnchor(). To these methods, you need to pass a double value representing the anchor.


The following program is an example of the Anchor Pane layout. In this, we are inserting a rotating cylinder in an anchor pane. At the same time, we are setting it at a distance of 50 units from the pane from all directions (Top, Left, Right, Bottom).

Save this code in a file with the name

import javafx.animation.RotateTransition; 
import javafx.collections.ObservableList; 
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane; 
import javafx.scene.paint.Color; 
import javafx.scene.paint.PhongMaterial; 
import javafx.scene.shape.Cylinder; 
import javafx.scene.transform.Rotate; 
import javafx.stage.Stage; 
import javafx.util.Duration;          

public class AnchorPaneExample extends Application { 
   public void start(Stage stage) {                     
      //Drawing a Cylinder 
      Cylinder cylinder = new Cylinder();        
      //Setting the properties of the Cylinder 
      //Preparing the phong material of type diffuse color 
      PhongMaterial material = new PhongMaterial();  
      //Setting the diffuse color material to Cylinder5 
      //Setting rotation transition for the cylinder 
      RotateTransition rotateTransition = new RotateTransition(); 
      //Setting the duration for the transition 
      //Setting the node for the transition 
      //Setting the axis of the rotation 
      //Setting the angle of the rotation 
      //Setting the cycle count for the transition 
      //Setting auto reverse value to false 
      //playing the animation; 
      //Creating an Anchor Pane  
      AnchorPane anchorPane = new AnchorPane();  
      //Setting the anchor to the cylinder      
      AnchorPane.setTopAnchor(cylinder, 50.0); 
      AnchorPane.setLeftAnchor(cylinder, 50.0); 
      AnchorPane.setRightAnchor(cylinder, 50.0); 
      AnchorPane.setBottomAnchor(cylinder, 50.0); 
      //Retrieving the observable list of the Anchor Pane 
      ObservableList list = anchorPane.getChildren();  
      //Adding cylinder to the pane 
      //Creating a scene object 
      Scene scene = new Scene(anchorPane);  
      //Setting title to the Stage 
      stage.setTitle("Anchor Pane Example"); 
      //Adding scene to the stage 
      //Displaying the contents of the stage; 
   public static void main(String args[]){ 

Compile and execute the saved java file from the command prompt using the following commands.

java AnchorPaneExample

On executing, the above program generates a JavaFX window as shown below.