I can’t figure out how to get a StackPane’s width :
If i refer to http://docs.oracle.com/javafx/2.0/api/javafx/scene/layout/StackPane.html, a StackPane’s dimensions should adapt to it’s content :
A stackpane’s parent will resize the stackpane within the stackpane’s resizable range during
layout. By default the stackpane computes this range based on its content as outlined in the
table below.preferredWidth left/right insets plus the largest of the children’s pref widths.
Syntax here is scala, but the issue concerns javafx :
import javafx.scene.layout.StackPane
import javafx.scene.shape.Rectangle
class Bubble() extends StackPane {
val rect = new Rectangle(100, 100)
getChildren.add(rect)
println(rect.getWidth)
println(this.getWidth)
}
Output :
>> 100.0
>> 0.0 <- Why isn't it 100 ?
Is this a bug or the awaited behaviour ? How can i get a StackPane’s content width ?
Thanx
Layout managers (and actually all resizable objects) don’t update their bounds until application being actually shown. Rectangle gives width 100 because it’s default value with your constructor.
See next code:
So you need either wait StackPane to be shown. Or better rely on JavaFX on that matter and use binding. E.g.