How to create an anchor pane using JavaFX?

Once you create all the required nodes for your application you can arrange them using a layout. Where a layout is the process of calculating the position of objects in the given space. JavaFX provides various layouts in the javafx.scene.layout package.

Anchor Pane

In this layout, the nodes are attached/anchored to a point (offset) from the edges of the pane.

You can create an anchor pane by instantiating the javafx.scene.layout.AnchorPane class. There are four anchor constraints to specify the distance from the edges of the panes to the edges of the nodes they are − topAnchor, leftAnchor, bottomAnchor, rightAnchor.

You can set the size of the anchor pane using the setPrefSize() method. To add nodes to this pane you can either pass them as arguments of the constructor or, add them to the observable list of the pane as −



import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.Separator;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
public class AnchorPaneExample extends Application {
   public void start(Stage stage) {
      //Creating the check boxes
      CheckBox checkBox1 = new CheckBox("Hindi");
      CheckBox checkBox2 = new CheckBox("Gujarathi");
      CheckBox checkBox3 = new CheckBox("Punjabi");
      CheckBox checkBox4 = new CheckBox("Telugu");
      CheckBox checkBox5 = new CheckBox("Tamil");
      CheckBox checkBox6= new CheckBox("Malayalam");
      //Creating a label
      Label label = new Label("Select known languages:");
      Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12);
      //Creating a separator
      Separator sep = new Separator();
      //Adding the check boxes and separator to the pane
      VBox vBox = new VBox(5);
      vBox.setPadding(new Insets(5, 5, 5, 210));
      vBox.getChildren().addAll(label, checkBox1, checkBox2, checkBox3, checkBox4, checkBox5, checkBox6);
      //Adding the separator after the 3rd check box
      vBox.getChildren().add(4, sep);
      //Creating the buttons
      Button next = new Button("Next");
      Button previous = new Button("previous");
      //Creating an Anchor Pane
      AnchorPane anchorPane = new AnchorPane();
      //Setting the anchor to the next and previous buttons
      AnchorPane.setRightAnchor(next, 5.0);
      AnchorPane.setLeftAnchor(previous, 5.0);
      //Retrieving the observable list of the Anchor Pane
      ObservableList list = anchorPane.getChildren();
      //Adding vBox and buttons to the anchor pane
      list.addAll(vBox, next, previous);
      //Setting the stage
      Scene scene = new Scene(anchorPane, 595, 200, Color.BEIGE);
      stage.setTitle("Anchor Pane Example");
   public static void main(String args[]){