AnchorPane allows the edges of child nodes to be anchored to an offset from the anchor pane's edges. If the anchor pane has a border and/or padding set, the offsets will be measured from the inside edge of those insets.
AnchorPane lays out each managed child regardless of the child's visible property value; unmanaged children are ignored for all layout calculations.
AnchorPanes may be styled with backgrounds and borders using CSS. See
Region
superclass for details.
The application sets anchor constraints on each child to configure the anchors on one or more sides. If a child is anchored on opposite sides (and is resizable), the anchor pane will resize it to maintain both offsets, otherwise the anchor pane will resize it to its preferred size. If in the former case (anchored on opposite sides) and the child is not resizable, then only the top/left anchor will be honored. AnchorPane provides a static method for setting each anchor constraint.
Constraint | Type | Description |
---|---|---|
topAnchor | double | distance from the anchor pane's top insets to the child's top edge. |
leftAnchor | double | distance from the anchor pane's left insets to the child's left edge. |
bottomAnchor | double | distance from the anchor pane's bottom insets to the child's bottom edge. |
rightAnchor | double | distance from the anchor pane's right insets to the child's right edge. |
AnchorPane Example:
AnchorPane anchorPane = new AnchorPane();
// List should stretch as anchorPane is resized
ListView list = new ListView();
AnchorPane.setTopAnchor(list, 10.0);
AnchorPane.setLeftAnchor(list, 10.0);
AnchorPane.setRightAnchor(list, 65.0);
// Button will float on right edge
Button button = new Button("Add");
AnchorPane.setTopAnchor(button, 10.0);
AnchorPane.setRightAnchor(button, 10.0);
anchorPane.getChildren().addAll(list, button);
An anchor pane's parent will resize the anchor pane within the anchor pane's resizable range during layout. By default the anchor pane computes this range based on its content as outlined in the table below.
width | height | |
---|---|---|
minimum | left/right insets plus width required to display children anchored at left/right with at least their min widths | top/bottom insets plus height required to display children anchored at top/bottom with at least their min heights |
preferred | left/right insets plus width required to display children anchored at left/right with at least their pref widths | top/bottom insets plus height required to display children anchored at top/bottom with at least their pref heights |
maximum | Double.MAX_VALUE | Double.MAX_VALUE |
An anchor pane's unbounded maximum width and height are an indication to the parent that it may be resized beyond its preferred size to fill whatever space is assigned to it.
AnchorPane provides properties for setting the size range directly. These properties default to the sentinel value Region.USE_COMPUTED_SIZE, however the application may set them to other values as needed:
anchorPane.setPrefSize(300, 300);
Applications may restore the computed values by setting these properties back
to Region.USE_COMPUTED_SIZE.
AnchorPane does not clip its content by default, so it is possible that childrens' bounds may extend outside its own bounds if the anchor pane is resized smaller than its preferred size.
extends