0

My code:

Button btn1 = new Button("Button");
    Button btn2 = new Button("The second Button");


    VBox vbox = new VBox();
    vbox.getChildren().addAll(btn1,btn2);
    Scene scene = new Scene(vbox, 300, 300);

    System.out.println(btn2.getBoundsInLocal());
    System.out.println(btn2.getBoundsInParent());
    System.out.println(btn2.getLayoutBounds());
    System.out.println(btn2.layoutBoundsProperty());

Print:

BoundingBox [minX:0.0, minY:0.0, minZ:0.0, width:0.0, height:0.0, depth:0.0, maxX:0.0, maxY:0.0, maxZ:0.0]
BoundingBox [minX:0.0, minY:0.0, minZ:0.0, width:0.0, height:0.0, depth:0.0, maxX:0.0, maxY:0.0, maxZ:0.0]
BoundingBox [minX:0.0, minY:0.0, minZ:0.0, width:0.0, height:0.0, depth:0.0, maxX:0.0, maxY:0.0, maxZ:0.0]
ReadOnlyObjectProperty [bean: Button@5f3c42b9[styleClass=button]'The second Button', name: layoutBounds, value: BoundingBox [minX:0.0, minY:0.0, minZ:0.0, width:0.0, height:0.0, depth:0.0, maxX:0.0, maxY:0.0, maxZ:0.0]]

My question is: When I get the coordinate of btn1, the MinX and MinY is 0, I can understand. but about btn2, the MinY should not be 0; and MaxX and MaxY also should not be 0. So how can I get the real coordinate of the left corner of the button in the VBox. To be clear, I need the coordinate of the node which is relative to VBox. Thanks.

fabian
  • 80,457
  • 12
  • 86
  • 114
A.Sheh
  • 61
  • 8

1 Answers1

1

I believe you have to get the parent bounds and then specify what you want relative to those.

System.out.println(btn2.getBoundsInParent().getY());
System.out.println(btn2.getBoundsInParent().getX());
Aniruddha Das
  • 20,520
  • 23
  • 96
  • 132
Aaron
  • 192
  • 6