2016-06-28 23 views
0

在我FXML我已經在AnchorPane得到了FlowPane:更改Node.height即刻Node.prefHeight(AnchorPane)

的AnchorPane應儘快FlowPane需要另一行改變其高度。我試圖在Flowpane.heightProperty上創建一個改變的偵聽器,它工作。然後,我將AnchorPane的高度設置爲flowPane的新高度,因爲我無法設置高度。問題是AnchorPane不能立即調整大小。 prefHeight改變,因爲我想它改變,但高度不是。在將鼠標拖過FlowPane之後,它會發生變化。

代碼:

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

我不能老是認爲我是第一個問這個問題。

回答

0

設置在底部和頂部錨0將導致AnchorPane的高度被自動調整大小,除非你有到位防止這種其他一些佈局限制:

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

注意實際大小調整Pane s在佈局過程中完成,可以手動觸發,請參閱jewelsea對此問題的回答:Get the height of a node in JavaFX (generate a layout pass)