Getting the width and height of the center space i

2019-07-04 03:01发布

I have a simple BorderPane instance which is the root node of my Scene. The border pane include a panel on the right side with some GUI controls, including a button. When I click on this button, I create an instance of class (Foo) which extends StackPane and I put this instance as a center node of the border pane.

I would like to get the width and height values of the available center space of the border pane from the Foo instance by calling this.getLayoutBounds.getWidth(), respectively ...getHeight() but it always returns zero?

标签: java javafx-2
1条回答
等我变得足够好
2楼-- · 2019-07-04 03:26

Add a listener to the StackPane's layoutBoundsProperty or bind to it's height and width properties.

JavaFX will just return zero for the height and width of the StackPane until it has executed a layout pass on it. Because a StackPane is a resizable node, JavaFX usually does not know what size it will be until it has been added to a scene and shown on a Stage.

If you only want to know it's initial size, you can call getHeight or getWidth on the StackPane after you have invoked stage.Show.

Future layout passes may resize the StackPane as it's available area changes, hence the recommendation to use listeners or binds if you want to always be informed of the current pane size.

查看更多
登录 后发表回答