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:
- AnchorPane(): Creates a new AnchorPane.
- 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:
- 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// stageimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.canvas.*;importjavafx.scene.web.*;importjavafx.scene.layout.AnchorPane;importjavafx.scene.shape.*;publicclassAnchorPane_1extendsApplication {// launch the applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("AnchorPane");// create a labelLabel label =newLabel("this is AnchorPane example");// create a AnchorPaneAnchorPane anchor_pane =newAnchorPane(label);// anchor to the labelAnchorPane.setTopAnchor(label,10.0);AnchorPane.setLeftAnchor(label,10.0);AnchorPane.setRightAnchor(label,10.0);AnchorPane.setBottomAnchor(label,10.0);// create a sceneScene scene =newScene(anchor_pane,400,300);// set the scenestage.setScene(scene);stage.show();}catch(Exception e) {System.out.println(e.getMessage());}}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}chevron_rightfilter_noneOutput:

- 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 stageimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.canvas.*;importjavafx.scene.web.*;importjavafx.scene.layout.AnchorPane;importjavafx.scene.shape.*;publicclassAnchorPane_2extendsApplication {// launch the applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("AnchorPane");// create a labelLabel label =newLabel("this is AnchorPane example");// create a AnchorPaneAnchorPane anchor_pane =newAnchorPane(label);// anchor to the labelAnchorPane.setTopAnchor(label,120.0);AnchorPane.setLeftAnchor(label,10.0);AnchorPane.setRightAnchor(label,230.0);AnchorPane.setBottomAnchor(label,120.0);Button button =newButton("button ");// anchor to the buttonAnchorPane.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 sceneScene scene =newScene(anchor_pane,400,300);// set the scenestage.setScene(scene);stage.show();}catch(Exception e) {System.out.println(e.getMessage());}}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}chevron_rightfilter_noneOutput:

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
Recommended Posts:
- JavaFX | Pos Class
- JavaFX | Tab Class
- JavaFX | SepiaTone Class
- JavaFX | ColorAdjust Class
- JavaFX | HTMLEditor Class
- JavaFX | FileChooser Class
- JavaFX | Hyperlink Class
- JavaFX | Slider Class
- JavaFX | Font Class
- JavaFX | FontPosture Class
- JavaFX | CycleMethod Class
- JavaFX | VLineTo Class
- JavaFX | ImagePattern Class
- JavaFX | LinearGradient Class
- JavaFX | Stop Class
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.



