An 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 BorderPane, an 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:
-
In an
AnchorPaneyou can anchor multiple nodes to one area (in aBorderPaneyou can only set one node to each area). -
In an
AnchorPaneyou can anchor one single node to multiple areas, i.e. anchoring aButtonto the left and the top of theAnchorPane, whereas in aBorderPaneevery node can only be applied to one area. -
In an
AnchorPaneyou can specify the offset to the anchor. -
A
BorderPaneprovides instance methods to populate each area (i.e.setCenter(Node node),setRight(Node node), ...). AnAnchorPaneoffers static methods likeAnchorPane.setTopAnchor(Node node, Double offset)that are applied directly to aNode. -
In a
BorderPaneeach child will grow and shrink with its parent depending on the area it is applied to. In anAnchorPanechildren will only be resized/stretched if they are anchored to opposite sides of the pane. For example if aButtonis anchored to the left and the right anchor of anAnchorPane, theButtonwill be stretched horizontally.
Please take a look at this first simple example: