JavaFX How to - Get node layout bound








Question

We would like to know how to get node layout bound.

Answer

// w w  w  .  ja  va 2s  . c o  m
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.effect.DropShadow;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {
  public static void main(String[] args) {
    Application.launch(args);
  }

  @Override
  public void start(Stage stage) {
    Button b1 = new Button("Close");
    b1.setEffect(new DropShadow());

    Button b3 = new Button("Close");
    b3.setEffect(new DropShadow());
    b3.setRotate(30);

    VBox root = new VBox();
    root.getChildren().addAll(b1, b3);

    Scene scene = new Scene(root);
    stage.setScene(scene);
    stage.setTitle("Testing LayoutBounds");
    stage.show();

    System.out.println("b1=" + b1.getLayoutBounds());
    System.out.println("b3=" + b3.getLayoutBounds());
  }
}