public class AnchorPane extends Pane
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 children's bounds may extend outside its own bounds if the anchor pane is resized smaller than its preferred size.
backgroundProperty, borderProperty, cacheShapeProperty, centerShapeProperty, heightProperty, insetsProperty, maxHeightProperty, maxWidthProperty, minHeightProperty, minWidthProperty, opaqueInsetsProperty, paddingProperty, prefHeightProperty, prefWidthProperty, scaleShapeProperty, shapeProperty, snapToPixelProperty, widthProperty
Modifier and Type | Field and Description |
---|---|
private static java.lang.String |
BOTTOM_ANCHOR |
private static java.lang.String |
LEFT_ANCHOR |
private static java.lang.String |
RIGHT_ANCHOR |
private static java.lang.String |
TOP_ANCHOR |
TEMP_VEC2D, USE_COMPUTED_SIZE, USE_PREF_SIZE
Constructor and Description |
---|
AnchorPane()
Creates an AnchorPane layout.
|
AnchorPane(Node... children)
Creates an AnchorPane layout with the given children.
|
Modifier and Type | Method and Description |
---|---|
static void |
clearConstraints(Node child)
Removes all anchor pane constraints from the child node.
|
private double |
computeChildHeight(Node child,
java.lang.Double topAnchor,
java.lang.Double bottomAnchor,
double areaHeight,
double width) |
private double |
computeChildWidth(Node child,
java.lang.Double leftAnchor,
java.lang.Double rightAnchor,
double areaWidth,
double height) |
private double |
computeHeight(boolean minimum,
double width) |
protected double |
computeMinHeight(double width)
Computes the minimum height of this region.
|
protected double |
computeMinWidth(double height)
Computes the minimum width of this region.
|
protected double |
computePrefHeight(double width)
Computes the preferred height of this region for the given width;
Region subclasses should override this method to return an appropriate
value based on their content and layout strategy.
|
protected double |
computePrefWidth(double height)
Computes the preferred width of this region for the given height.
|
private double |
computeWidth(boolean minimum,
double height) |
static java.lang.Double |
getBottomAnchor(Node child)
Returns the child's bottom anchor constraint if set.
|
static java.lang.Double |
getLeftAnchor(Node child)
Returns the child's left anchor constraint if set.
|
static java.lang.Double |
getRightAnchor(Node child)
Returns the child's right anchor constraint if set.
|
static java.lang.Double |
getTopAnchor(Node child)
Returns the child's top anchor constraint if set.
|
protected void |
layoutChildren() |
static void |
setBottomAnchor(Node child,
java.lang.Double value)
Sets the bottom anchor for the child when contained by an anchor pane.
|
static void |
setLeftAnchor(Node child,
java.lang.Double value)
Sets the left anchor for the child when contained by an anchor pane.
|
static void |
setRightAnchor(Node child,
java.lang.Double value)
Sets the right anchor for the child when contained by an anchor pane.
|
static void |
setTopAnchor(Node child,
java.lang.Double value)
Sets the top anchor for the child when contained by an anchor pane.
|
getChildren, getConstraint, setConstraint
addImageListener, adjustHeightByMargin, adjustWidthByMargin, backgroundProperty, borderProperty, boundedNodeSizeWithBias, boundedSize, cacheShapeProperty, centerShapeProperty, computeChildMaxAreaHeight, computeChildMaxAreaWidth, computeChildMinAreaHeight, computeChildMinAreaHeight, computeChildMinAreaWidth, computeChildMinAreaWidth, computeChildPrefAreaHeight, computeChildPrefAreaHeight, computeChildPrefAreaWidth, computeChildPrefAreaWidth, computeMaxHeight, computeMaxMinAreaHeight, computeMaxMinAreaHeight, computeMaxMinAreaHeight, computeMaxMinAreaWidth, computeMaxMinAreaWidth, computeMaxMinAreaWidth, computeMaxPrefAreaHeight, computeMaxPrefAreaHeight, computeMaxPrefAreaHeight, computeMaxPrefAreaWidth, computeMaxPrefAreaWidth, computeMaxPrefAreaWidth, computeMaxWidth, computeXOffset, computeYOffset, createDoubleArray, getAreaBaselineOffset, getAreaBaselineOffset, getAreaBaselineOffset, getAreaBaselineOffset, getAreaBaselineOffset, getAreaBaselineOffset, getBackground, getBorder, getClassCssMetaData, getCssMetaData, getHeight, getInsets, getMaxBaselineComplement, getMaxHeight, getMaxWidth, getMinBaselineComplement, getMinHeight, getMinWidth, getOpaqueInsets, getPadding, getPrefBaselineComplement, getPrefHeight, getPrefWidth, getShape, getUserAgentStylesheet, getWidth, heightProperty, insetsProperty, isCacheShape, isCenterShape, isResizable, isScaleShape, isSnapToPixel, layoutInArea, layoutInArea, layoutInArea, layoutInArea, maxHeight, maxHeightProperty, maxWidth, maxWidthProperty, minHeight, minHeightProperty, minWidth, minWidthProperty, opaqueInsetsProperty, paddingProperty, positionInArea, positionInArea, prefHeight, prefHeightProperty, prefWidth, prefWidthProperty, removeImageListener, resize, scaleShapeProperty, setBackground, setBorder, setCacheShape, setCenterShape, setHeight, setMaxHeight, setMaxSize, setMaxWidth, setMinHeight, setMinSize, setMinWidth, setOpaqueInsets, setPadding, setPrefHeight, setPrefSize, setPrefWidth, setScaleShape, setShape, setSnapToPixel, setWidth, shapeProperty, snappedBottomInset, snappedLeftInset, snappedRightInset, snappedTopInset, snapPortionX, snapPortionY, snapPosition, snapPositionX, snapPositionY, snapSize, snapSizeX, snapSizeY, snapSpace, snapSpaceX, snapSpaceY, snapToPixelProperty, widthProperty
private static final java.lang.String TOP_ANCHOR
private static final java.lang.String LEFT_ANCHOR
private static final java.lang.String BOTTOM_ANCHOR
private static final java.lang.String RIGHT_ANCHOR
public AnchorPane()
public AnchorPane(Node... children)
children
- The initial set of children for this pane.public static void setTopAnchor(Node child, java.lang.Double value)
child
- the child node of an anchor panevalue
- the offset from the top of the anchor panepublic static java.lang.Double getTopAnchor(Node child)
child
- the child node of an anchor panepublic static void setLeftAnchor(Node child, java.lang.Double value)
child
- the child node of an anchor panevalue
- the offset from the left of the anchor panepublic static java.lang.Double getLeftAnchor(Node child)
child
- the child node of an anchor panepublic static void setBottomAnchor(Node child, java.lang.Double value)
child
- the child node of an anchor panevalue
- the offset from the bottom of the anchor panepublic static java.lang.Double getBottomAnchor(Node child)
child
- the child node of an anchor panepublic static void setRightAnchor(Node child, java.lang.Double value)
child
- the child node of an anchor panevalue
- the offset from the right of the anchor panepublic static java.lang.Double getRightAnchor(Node child)
child
- the child node of an anchor panepublic static void clearConstraints(Node child)
child
- the child nodeprotected double computeMinWidth(double height)
Region
computeMinWidth
in class Region
protected double computeMinHeight(double width)
Region
computeMinHeight
in class Region
protected double computePrefWidth(double height)
Region
computePrefWidth
in class Region
protected double computePrefHeight(double width)
Region
computePrefHeight
in class Region
private double computeWidth(boolean minimum, double height)
private double computeHeight(boolean minimum, double width)
private double computeChildWidth(Node child, java.lang.Double leftAnchor, java.lang.Double rightAnchor, double areaWidth, double height)
private double computeChildHeight(Node child, java.lang.Double topAnchor, java.lang.Double bottomAnchor, double areaHeight, double width)
protected void layoutChildren()