Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problem getting Label width

ytwApr 18 2012 — edited Apr 19 2012
Hello,

I can't get a Label's width with the following test code.


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

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

@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Label width test");

StackPane sp = new StackPane();

Label label = new Label();
label.setText("test");

sp.getChildren().add(label);

double width = label.getWidth();
System.out.println(width); // always prints 0

Scene scene = new Scene(sp, 800, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
}


Does anyone know why?

Thanks for your help.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 17 2012
Added on Apr 18 2012
5 comments
1,491 views