Getting the width and height of the center space in a BorderPane (JavaFX 2)

我的未来我决定 提交于 2020-01-04 04:40:09

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/12643125/getting-the-width-and-height-of-the-center-space-in-a-borderpane-javafx-2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!