JavaFX | AnchorPane Class

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:

  1. AnchorPane(): Creates a new AnchorPane.
  2. AnchorPane(Node… c): Creates a AnchorPane with specified nodes.

Commonly Used Methods:



Method Explanation
getBottomAnchor(Node c) Returns the child’s bottom anchor.
getLeftAnchor(Node c) Returns the child’s left anchor.
getRightAnchor(Node c) Returns the child’s right anchor.
getTopAnchor(Node c) Returns the child’s top anchor.
setBottomAnchor(Node c, Double v) Sets the child’s bottom anchor.
setLeftAnchor(Node c, Double v) Sets the child’s left anchor.
setRightAnchor(Node c, Double v) Sets the child’s right anchor.
setTopAnchor(Node c, Double v) Sets the child’s top anchor.

Below programs illustrate the use of the AnchorPane Class:

  1. 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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program to create a AnchorPane and
    // add label to it and add label to the 
    // stage
    import 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);
        }
    }

    chevron_right

    
    

    Output:

  2. 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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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
    import 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);
        }
    }

    chevron_right

    
    

    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



My Personal Notes arrow_drop_up

Second year Department of Information Technology Jadavpur University

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.