hello friends i have a problem with javafx AnchorPane. I have an anchorpane that contains a button i want to set the height and width of anchorpane to zero but it no apply. how to do this .... actually i am working on a sliding panel

Dani AI

Generated

Short answer: the AnchorPane (and other JavaFX layout panes) won’t collapse to (0,0) if its managed children or its own min/pref constraints prevent it. AnchorPane lays out every managed child (even if that child is invisible) and a parent’s preferred/minimum size is computed from those children. That is why calling setPrefSize(0,0) alone often appears to do nothing. AnchorPane javadoc, Node managed docs.

Fast fixes (no animation)

  • Remove the panel from the scene graph, or make it unmanaged and not visible so the layout ignores it while you can later restore it.
  • Example:
    panel.setVisible(false);
    panel.setManaged(false);
    // or keep them in sync:
    panel.managedProperty().bind(panel.visibleProperty());

    Making a node unmanaged removes it from parent layout calculations. Practical explanation and community example here: hide without occupying space (StackOverflow).

Animated sliding / collapse

  • Animate the pane’s prefWidthProperty() (or prefHeightProperty()), but first allow it to shrink by setting minWidth/minHeight to 0 and make sure children can shrink (many Controls have non-zero computed minimums).
    
    panel.setMinWidth(0);
    for (Node c : panel.getChildren()) {
    if (c instanceof Region) ((Region)c).setMinWidth(0);
    }

Timeline close = new Timeline(
new KeyFrame(Duration.ZERO, new KeyValue(panel.prefWidthProperty(), panel.getWidth())),
new KeyFrame(Duration.millis(250), new KeyValue(panel.prefWidthProperty(), 0))
);
close.play();

- AnchorPane does not clip children by default, so bind a clip if children should not draw outside the shrinking bounds:
```java
Rectangle clip = new Rectangle();
clip.widthProperty().bind(panel.widthProperty());
clip.heightProperty().bind(panel.heightProperty());
panel.setClip(clip);

References: Region sizing methods (setMinWidth, setPrefWidth) and animation example using prefWidthProperty() are documented/illustrated in the official Javadocs and community answers. Region javadoc, Timeline example (StackOverflow) (https://stackoverflow.com/questions/46930570/javafx-animation-scaletransition).

Troubleshooting checklist

  • Check child min/pref sizes (controls often force a minimum).
  • If a child is anchored on both left+right, AnchorPane will resize that child instead of letting the container collapse—clear or adjust those anchors.
  • If only a visual move is needed (no reflow), use a TranslateTransition instead of changing layout size.
    As suggested, the docs explain the layout/managed rules cited above; follow those rules to decide whether to hide, un-manage, translate, or animate pref-size.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.