AnchorPane is similar to a
BorderPane (see my previous post JavaFX 2.0 Layout Panes - BorderPane) as it also provides several areas or regions for your nodes.
In contrast to a
AnchorPane provides only four different regions: Left, Right, Bottom and Top (a
BorderPane additionally provides a center region).
There are several more major differences between the two panes:
AnchorPaneyou can anchor multiple nodes to one area (in a
BorderPaneyou can only set one node to each area).
AnchorPaneyou can anchor one single node to multiple areas, i.e. anchoring a
Buttonto the left and the top of the
AnchorPane, whereas in a
BorderPaneevery node can only be applied to one area.
AnchorPaneyou can specify the offset to the anchor.
BorderPaneprovides instance methods to populate each area (i.e.
setRight(Node node), ...). An
AnchorPaneoffers static methods like
AnchorPane.setTopAnchor(Node node, Double offset)that are applied directly to a
BorderPaneeach child will grow and shrink with its parent depending on the area it is applied to. In an
AnchorPanechildren will only be resized/stretched if they are anchored to opposite sides of the pane. For example if a
Buttonis anchored to the left and the right anchor of an
Buttonwill be stretched horizontally.
Please take a look at this first simple example: