Updated April 1, 2023
Introduction to JavaFX AnchorPane
The JavaFX Anchor Pane is used for anchoring to an offset from an anchor pane edges by its child node edges. If the anchor pane has a border or padding set, the off sets is measuring from the inside corner edges of the insets. Anchor pane lay out managing child of the child’s makes visible-property value and non-managing child are not considered for calculating layouts. We have facility to add CSS styles with Anchor pane of JavaFX. Anchor Pane is a JavaFX class, which is available in scene.layout.AnchorPane package. We can apply JavaFX Anchor pane with label, cylinder structure, rectangle structure, buttons etc.
Constructors
- AnchorPane(): Creates an anchor pane object with new keyword without any argument in the constructor.
- AnchorPane(Node n1, Node n2, Node n3,….): Creates an anchor pane object with new keyword with required number of arguments in the constructor.
Frequently Used Methods
Below are the used methods:
- setBottomAnchor(Node b, Double value): setBottomAnchor() method is used to set Bottom anchor.
- getBottomAnchor(Node b): getBottomAnchor() method returns bottom anchor.
- setLeftAnchor(Node l, Double value): setLeftAnchor() method is used to set left anchor.
- getLeftAnchor(Node l): getLeftAnchor() method returns left anchor.
- setRightAnchor(Node r, Double value): setRightAnchor() method is used to set right anchor.
- getRightAnchor(Node r): getRightAnchor() method returns right anchor.
- setTopAnchor(Node t, Double value): setRightAnchor() method is used to set top anchor.
- getTopAnchor(Node t): getTopAnchor() method returns top anchor.
How does AnchorPane work in JavaFX?
Accessing JavaFX features user defined class must extends Application class.
1. In JavaFX creating AnchorPane is first step. AnchorPane can instantiate by using new keyword.
Syntax:
AnchorPane anchorPaneRef=new AnchorPane();
2. Creating Label or Button or any JavaFX element is second step.
Syntax:
Label lableRef=new Label(“Any Message”);
anchorPaneRef.setTopAnchor(labelRef, value, value);
3. Create VBox or any other display(like TilePane or HBox as per requirement) class to add the items is third step.
Syntax:
VBox vBox=new VBox(anchorPaneRef); //Gives vertical box
4. Fourth for apply show method on to it.
Syntax:
Step is creating scene.
Scene screen = new Scene(vBox, length, width);
5. Adding Scene reference screen to the Stage object reference is fifth step. Adding output screen to Stage. We will get this stage object reference from start predefined JavaFX method.
Syntax:
stage.setScene(screen);
6. Sixth step is showing output to the end user by applying show() method on the scene object.
Syntax:
stage.show();
Examples of JavaFX AnchorPane
Given below are the examples:
Example #1
Anchor pane with Label.
Code:
package com.anchorpanae;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
//importing AnchorPane class
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
public class AnchorPaneLabel extends Application {
// launch method invoked this start method
public void start(Stage outStage) {
try {
// setting the tile for application
outStage.setTitle("AnchorPane Label");
// create the label for adding the text to the application
Label lableAnchor = new Label("I am anchor pane lable to show the demo.");
// creating a AnchorPane object
AnchorPane anchorPaneObject = new AnchorPane(lableAnchor);
//set the label to top anchor
AnchorPane.setTopAnchor(lableAnchor, 20.0);
//set the label to left anchor
AnchorPane.setLeftAnchor(lableAnchor, 100.0);
//set the label to right anchor
AnchorPane.setRightAnchor(lableAnchor, 20.0);
//set the label to bottom anchor
AnchorPane.setBottomAnchor(lableAnchor, 20.0);
// creating the scene to display output
Scene screen = new Scene(anchorPaneObject, 410, 310);
outStage.setScene(screen);
// display the output
outStage.show();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
// Main Method
public static void main(String args[]) {
// invoking main method from JVM
launch(args);
}
}
Output:
Example #2
Anchor pane with Buttons.
Code:
package com.anchorpanae;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
//importing AnchorPane class
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class AnchorPaneButton extends Application {
// launch method invoked this start method
public void start(Stage outStage) {
try {
// setting the tile for application
outStage.setTitle("AnchorPane Buttons");
// creating the buttons for adding the text to the application
Button button1 = new Button("First Button");
Button button2 = new Button("Second Button");
// creating a AnchorPane object
AnchorPane anchorPaneObject1 = new AnchorPane(button1);
//set the label to top anchor
anchorPaneObject1.setTopAnchor(button1, 125.0);
//set the label to left anchor
anchorPaneObject1.setLeftAnchor(button1, 200.0);
//set the label to right anchor
anchorPaneObject1.setRightAnchor(button1, 100.0);
//set the label to bottom anchor
anchorPaneObject1.setBottomAnchor(button1, 500.0);
//anchorPaneObject.getChildren().add(button1);
anchorPaneObject1.setMinHeight(500);
anchorPaneObject1.setMinWidth(500);
// creating a AnchorPane object
AnchorPane anchorPaneObject2 = new AnchorPane(button2);
//set the label to top anchor
anchorPaneObject2.setTopAnchor(button2, 150.0);
//set the label to left anchor
anchorPaneObject2.setLeftAnchor(button2, 200.0);
//set the label to right anchor
anchorPaneObject2.setRightAnchor(button2, 100.0);
//set the label to bottom anchor
anchorPaneObject2.setBottomAnchor(button2, 500.0);
//anchorPaneObject.getChildren().add(button1);
anchorPaneObject2.setMinHeight(500);
anchorPaneObject2.setMinWidth(500)
//creating VBox for adding anchor panes
VBox vBox=new VBox();
vBox.getChildren().add(anchorPaneObject1);
vBox.getChildren().add(anchorPaneObject2);
//scrollpane for scrolling
ScrollPane scrollPnae=new ScrollPane();
scrollPnae.setContent(vBox);
// creating the scene to display output
Scene screen = new Scene(scrollPnae, 400, 300);
outStage.setScene(screen);
// display the output
outStage.show();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
// Main Method
public static void main(String args[]) {
// invoking main method from JVM
launch(args);
}
}
Output:
Example #3
Anchor pane with Rectangle shape.
Code:
package com.anchorpanae;
import javafx.application.Application;
import javafx.scene.Scene;
//importing AnchorPane class
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class AnchorPaneImage extends Application {
// launch method invoked this start method
public void start(Stage outStage) {
try {
// setting the tile for application
outStage.setTitle("AnchorPane Rectangle Shape");
//creating rectangle shape
Rectangle rectangleShape = new Rectangle(300,200,Color.GREEN);
rectangleShape.relocate(70,70);
// creating a AnchorPane object
AnchorPane anchorPaneObject = new AnchorPane(rectangleShape);
//set the label to top anchor
AnchorPane.setTopAnchor(rectangleShape, 20.0);
//set the label to left anchor
AnchorPane.setLeftAnchor(rectangleShape, 100.0);
//set the label to right anchor
AnchorPane.setRightAnchor(rectangleShape, 20.0);
//set the label to bottom anchor
AnchorPane.setBottomAnchor(rectangleShape, 20.0);
// creating the scene to display output
Scene screen = new Scene(anchorPaneObject, 500, 400);
outStage.setScene(screen);
// display the output
outStage.show();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
}
// Main Method
public static void main(String args[]) {
// invoking main method from JVM
launch(args);
}
}
Output:
Recommended Articles
This is a guide to JavaFX AnchorPane. Here we discuss the introduction, how does AnchorPane work in JavaFX along with constructors, frequently used methods and examples. You may also have a look at the following articles to learn more –