Get stack width

I cannot figure out how to get the width of the stackpack:

If I refer to http://docs.oracle.com/javafx/2.0/api/javafx/scene/layout/StackPane.html , the dimensions of the StackPane should adapt the contents to it:

The parent of the stack will resize the stack within the resizable stack layout. By default, the stack stack calculates this range based on its contents, as indicated in the following.

preferredWidth left / right insets plus the largest pref width for children.

The syntax here is scala, but the problem is with 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 an expected behavior? How can I get the width of the contents of a stackpack?

Thanx

+3
source share
1 answer

( ) , . Rectangle 100, .

:

@Override
public void start(Stage stage) throws Exception {
    StackPane root = new StackPane();
    Rectangle rect = new Rectangle(100, 100);
    root.getChildren().add(rect);

    // 100 - 0
    System.out.println(rect.getWidth());
    System.out.println(root.getWidth());

    stage.setScene(new Scene(root));

    // 100 - 0
    System.out.println(rect.getWidth());
    System.out.println(root.getWidth());

    stage.show();

    // 100 - 100
    System.out.println(rect.getWidth());
    System.out.println(root.getWidth());
}

, , StackPane . JavaFX . .

    stage.titleProperty().bind(root.widthProperty().asString());
+3

All Articles