0

In my fxml I have got a FlowPane in an AnchorPane:

The AnchorPane should change its height as soon as the FlowPane needs another line. I tried to make a changed listener on the Flowpane.heightProperty and it worked. Then I set the prefHeight of the AnchorPane to the new height of the flowPane because I can`t set the height. The problem is that the AnchorPane does not resize instantly. The prefHeight changes as I want it to change, but the height not. It changes after I drag my mouse over the FlowPane.

Code:

FlowPane flowPane = new FlowPane();
AnchorPane anchorPane = new AnchorPane(flowPane);

titleAnchorPane.setTopAnchor(titleFlowPane, 0.0);
titleAnchorPane.setLeftAnchor(titleFlowPane, 0.0);
titleAnchorPane.setRightAnchor(titleFlowPane, 0.0);

flowPane.heightProperty().addListener((observable, oldValue, newValue) -> {
            anchorPane.setPrefHeight(newValue.doubleValue()); 
 });

Bevore I dragged my mouse over it

After I dragged my mouse over it

I can`t believe that I am the first one asking this question.

CODEFTW
  • 23
  • 3

1 Answers1

0

Setting the bottom and top anchors to 0 will result in the AnchorPane's height being resized automatically, unless you've got some other layout constraints in place preventing this:

AnchorPane.setTopAnchor(flowPane, 0.0);
AnchorPane.setBottomAnchor(flowPane, 0.0);

Note that the actual resizing of Panes is done during the layout pass, which can be triggered manually see jewelsea's answer to this question: Get the height of a node in JavaFX (generate a layout pass) .

Community
  • 1
  • 1
fabian
  • 80,457
  • 12
  • 86
  • 114