JavaFX | AnchorPane Class

11 Sep, 2018
AnchorPane class is a part of JavaFX. 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. AnchorPane inherits Pane class.
Constructors of the class:
AnchorPane(): Creates a new AnchorPane.AnchorPane(Node... c): Creates a AnchorPane with specified nodes.
AnchorPane(): Creates a new AnchorPane.
AnchorPane(Node... c): Creates a AnchorPane with specified nodes.
Commonly Used Methods:
Below programs illustrate the use of the AnchorPane Class:
Java Program to create a AnchorPane and add label to it and add label to the stage: In this program we will create a AnchorPane named anchor_pane. Add a Label named label to the anchor_pane and set the top, bottom, left, right using the setTopAnchor(), setBottomAnchor(), setLeftAnchor(), setRightAnchor() functions respectively. Now add the anchor_pane to the Scene. Then finally add the scene to the stage and call the show() function to display the results.// Java Program to create a AnchorPane and// add label to it and add label to the // stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.canvas.*;import javafx.scene.web.*;import javafx.scene.layout.AnchorPane;import javafx.scene.shape.*;  public class AnchorPane_1 extends Application {      // launch the application    public void start(Stage stage)    {          try {              // set title for the stage            stage.setTitle("AnchorPane");              // create a label            Label label = new Label("this is AnchorPane example");              // create a AnchorPane            AnchorPane anchor_pane = new AnchorPane(label);              // anchor to the label            AnchorPane.setTopAnchor(label, 10.0);            AnchorPane.setLeftAnchor(label, 10.0);            AnchorPane.setRightAnchor(label, 10.0);            AnchorPane.setBottomAnchor(label, 10.0);              // create a scene            Scene scene = new Scene(anchor_pane, 400, 300);              // set the scene            stage.setScene(scene);              stage.show();        }          catch (Exception e) {              System.out.println(e.getMessage());        }    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}Output:Java Program to create a AnchorPane, adding label and button to it and also setting the min height and width of AnchorPane then add it to the stage: In this program we will create a AnchorPane named anchor_pane. Add a Label named label to the anchor_pane. Also add a Button named button and set the top, bottom, left, right anchor using the setTopAnchor(), setBottomAnchor(), setLeftAnchor(), setRightAnchor() functions respectively. Set the min height and width using the setMinHeight() and setMinWidth() function. Add the anchor_pane to the Scene. Finally, add the scene to the stage and call the show() function to display the results.// Java Program to create a AnchorPane, adding// label and button to it and also setting the // min height and width of AnchorPane then add// it to the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.canvas.*;import javafx.scene.web.*;import javafx.scene.layout.AnchorPane;import javafx.scene.shape.*;  public class AnchorPane_2 extends Application {      // launch the application    public void start(Stage stage)    {          try {              // set title for the stage            stage.setTitle("AnchorPane");              // create a label            Label label = new Label("this is AnchorPane example");              // create a AnchorPane            AnchorPane anchor_pane = new AnchorPane(label);              // anchor to the label            AnchorPane.setTopAnchor(label, 120.0);            AnchorPane.setLeftAnchor(label, 10.0);            AnchorPane.setRightAnchor(label, 230.0);            AnchorPane.setBottomAnchor(label, 120.0);              Button button = new Button("button ");              // anchor to the button            AnchorPane.setTopAnchor(button, 125.0);            AnchorPane.setLeftAnchor(button, 220.0);            AnchorPane.setRightAnchor(button, 110.0);            AnchorPane.setBottomAnchor(button, 125.0);              anchor_pane.getChildren().add(button);              anchor_pane.setMinHeight(400);            anchor_pane.setMinWidth(400);              // create a scene            Scene scene = new Scene(anchor_pane, 400, 300);              // set the scene            stage.setScene(scene);              stage.show();        }          catch (Exception e) {              System.out.println(e.getMessage());        }    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}Output:
Java Program to create a AnchorPane and add label to it and add label to the stage: In this program we will create a AnchorPane named anchor_pane. Add a Label named label to the anchor_pane and set the top, bottom, left, right using the setTopAnchor(), setBottomAnchor(), setLeftAnchor(), setRightAnchor() functions respectively. Now add the anchor_pane to the Scene. Then finally add the scene to the stage and call the show() function to display the results.// Java Program to create a AnchorPane and// add label to it and add label to the // stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.canvas.*;import javafx.scene.web.*;import javafx.scene.layout.AnchorPane;import javafx.scene.shape.*;  public class AnchorPane_1 extends Application {      // launch the application    public void start(Stage stage)    {          try {              // set title for the stage            stage.setTitle("AnchorPane");              // create a label            Label label = new Label("this is AnchorPane example");              // create a AnchorPane            AnchorPane anchor_pane = new AnchorPane(label);              // anchor to the label            AnchorPane.setTopAnchor(label, 10.0);            AnchorPane.setLeftAnchor(label, 10.0);            AnchorPane.setRightAnchor(label, 10.0);            AnchorPane.setBottomAnchor(label, 10.0);              // create a scene            Scene scene = new Scene(anchor_pane, 400, 300);              // set the scene            stage.setScene(scene);              stage.show();        }          catch (Exception e) {              System.out.println(e.getMessage());        }    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}Output:
// Java Program to create a AnchorPane and// add label to it and add label to the // stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.canvas.*;import javafx.scene.web.*;import javafx.scene.layout.AnchorPane;import javafx.scene.shape.*;  public class AnchorPane_1 extends Application {      // launch the application    public void start(Stage stage)    {          try {              // set title for the stage            stage.setTitle("AnchorPane");              // create a label            Label label = new Label("this is AnchorPane example");              // create a AnchorPane            AnchorPane anchor_pane = new AnchorPane(label);              // anchor to the label            AnchorPane.setTopAnchor(label, 10.0);            AnchorPane.setLeftAnchor(label, 10.0);            AnchorPane.setRightAnchor(label, 10.0);            AnchorPane.setBottomAnchor(label, 10.0);              // create a scene            Scene scene = new Scene(anchor_pane, 400, 300);              // set the scene            stage.setScene(scene);              stage.show();        }          catch (Exception e) {              System.out.println(e.getMessage());        }    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}
Output:
Java Program to create a AnchorPane, adding label and button to it and also setting the min height and width of AnchorPane then add it to the stage: In this program we will create a AnchorPane named anchor_pane. Add a Label named label to the anchor_pane. Also add a Button named button and set the top, bottom, left, right anchor using the setTopAnchor(), setBottomAnchor(), setLeftAnchor(), setRightAnchor() functions respectively. Set the min height and width using the setMinHeight() and setMinWidth() function. Add the anchor_pane to the Scene. Finally, add the scene to the stage and call the show() function to display the results.// Java Program to create a AnchorPane, adding// label and button to it and also setting the // min height and width of AnchorPane then add// it to the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.canvas.*;import javafx.scene.web.*;import javafx.scene.layout.AnchorPane;import javafx.scene.shape.*;  public class AnchorPane_2 extends Application {      // launch the application    public void start(Stage stage)    {          try {              // set title for the stage            stage.setTitle("AnchorPane");              // create a label            Label label = new Label("this is AnchorPane example");              // create a AnchorPane            AnchorPane anchor_pane = new AnchorPane(label);              // anchor to the label            AnchorPane.setTopAnchor(label, 120.0);            AnchorPane.setLeftAnchor(label, 10.0);            AnchorPane.setRightAnchor(label, 230.0);            AnchorPane.setBottomAnchor(label, 120.0);              Button button = new Button("button ");              // anchor to the button            AnchorPane.setTopAnchor(button, 125.0);            AnchorPane.setLeftAnchor(button, 220.0);            AnchorPane.setRightAnchor(button, 110.0);            AnchorPane.setBottomAnchor(button, 125.0);              anchor_pane.getChildren().add(button);              anchor_pane.setMinHeight(400);            anchor_pane.setMinWidth(400);              // create a scene            Scene scene = new Scene(anchor_pane, 400, 300);              // set the scene            stage.setScene(scene);              stage.show();        }          catch (Exception e) {              System.out.println(e.getMessage());        }    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}Output:
// Java Program to create a AnchorPane, adding// label and button to it and also setting the // min height and width of AnchorPane then add// it to the stageimport javafx.application.Application;import javafx.scene.Scene;import javafx.scene.control.*;import javafx.scene.layout.*;import javafx.stage.Stage;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.canvas.*;import javafx.scene.web.*;import javafx.scene.layout.AnchorPane;import javafx.scene.shape.*;  public class AnchorPane_2 extends Application {      // launch the application    public void start(Stage stage)    {          try {              // set title for the stage            stage.setTitle("AnchorPane");              // create a label            Label label = new Label("this is AnchorPane example");              // create a AnchorPane            AnchorPane anchor_pane = new AnchorPane(label);              // anchor to the label            AnchorPane.setTopAnchor(label, 120.0);            AnchorPane.setLeftAnchor(label, 10.0);            AnchorPane.setRightAnchor(label, 230.0);            AnchorPane.setBottomAnchor(label, 120.0);              Button button = new Button("button ");              // anchor to the button            AnchorPane.setTopAnchor(button, 125.0);            AnchorPane.setLeftAnchor(button, 220.0);            AnchorPane.setRightAnchor(button, 110.0);            AnchorPane.setBottomAnchor(button, 125.0);              anchor_pane.getChildren().add(button);              anchor_pane.setMinHeight(400);            anchor_pane.setMinWidth(400);              // create a scene            Scene scene = new Scene(anchor_pane, 400, 300);              // set the scene            stage.setScene(scene);              stage.show();        }          catch (Exception e) {              System.out.println(e.getMessage());        }    }      // Main Method    public static void main(String args[])    {          // launch the application        launch(args);    }}
Output:
Note: The above programs might not run in an online IDE please use an offline compiler.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/AnchorPane.html
JavaFX
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Introduction to Java
Constructors in Java
Exceptions in Java
Generics in Java
Functional Interfaces in Java
Strings in Java
Java Programming Examples
HashSet in Java
Abstraction in Java