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