0

How to get the dimensions of a Label before it is painted in the scene?. JavaFX 19 In some previous version this same code worked correctly.

import javafx.application.Application;
import javafx.geometry.Bounds;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

    public class LabelBoundsExample extends Application {
    
        @Override
        public void start(Stage primaryStage) throws Exception {
            Label label = new Label("Hello World!");
            StackPane root = new StackPane(label);
            Scene scene = new Scene(root, 400, 400);
     
            Bounds labelBounds = label.getBoundsInLocal();
              
            System.out.println("Label width: " + labelBounds.getWidth());
            System.out.println("Label height: " + labelBounds.getHeight());
    
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
Tretonis
  • 27
  • 5

0 Answers0