I read this old answer on how to accomplish this. But since it involves using impl_processCSS(boolean)
, a method that is now deprecated, I think we need to update the answer.
I've tried placing the label inside a HBox and then get the size of it, or getting the size of the HBox, without any luck. And I've also tried using .label.getBoundsInLocal().getWidth().
SSCCE: import javafx.scene.control.Label; import javafx.scene.layout.HBox; import javafx.application.Application; import javafx.scene.Scene;
public class SSCCE extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
HBox root = new HBox();
Label label = new Label("foo");
System.out.println(label.getWidth());
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
}