How to get label.getWidth () in javafx

whenever I try to get the width of an array in Java, it just returns 0, and I don't know why. Can someone explain to me how this is done correctly?

                Label label = new Label();
                label.setFont(Font.font(label.getFont().getFamily(), 8));
                label.setText(""
                        + a[i][j].getList().getFirst().getLength()
                        + " mal "
                        + intToColor(a[i][j].getList().getFirst()
                                .getColor()));
                label.relocate((x1 + x2) / 2 - label.getWidth() / 2, (y1 + y2) / 2);
                label.idProperty().set("trackName");
                label.setTextFill(Color.web("GREEN"));
                field.getChildren().addAll(path, label);
                System.out.println(label.getLayoutBounds().getWidth());
                System.out.println(label.getWidth());//... also i tested a lot of different getters but i couldn't get the label width (same problem with height)

Hope you have an idea what I should do.

@tomsontom

did this:

                label.prefWidth(-1);
                label.prefHeight(-1);
                label.impl_processCSS(true);
//                  label.resizeRelocate(x, y, width, height);
                System.out.println(label.getWidth());

but it didn’t work - can you explain to me more precisely what I need to do?

+4
source share
4 answers

To get the width needed to call prefWidth (-1) and prefHeight (-1), layout boundaries are set only after the control is rendered through resizeRelocate

, impl_processCSS (true), API- NONE, . IIRC

 HBox h = new HBox();
 Label l = new Label("Hello");
 h.getChildren().add(l);
 Scene s = new Scene(h);
 l.impl_processCSS(true);
 System.err.println(l.prefWidth(-1)+"/"+l.prefHeight(-1));
+7

textProperty Fontloader.computeStringWidth(text, font), textProperty .

:

    FontLoader fontLoader = Toolkit.getToolkit().getFontLoader();
    Label label = new Label("My name is Warren. I love Java.");
    label.setFont(Font.font("Consolas", FontWeight.THIN, FontPosture.REGULAR, 16));
    System.out.println("The label width is: " + fontLoader.computeStringWidth(label.getText(), label.getFont()));

:

The label width is: 272.70312

:

    FontLoader fontLoader = Toolkit.getToolkit().getFontLoader();
    Label label = new Label("My name is Warren. I love Java.");
    label.setFont(Font.font("Consolas", FontWeight.THIN, FontPosture.REGULAR, 16));
    System.out.println("The label textProperty string width is: " + fontLoader.computeStringWidth(label.getText(), label.getFont()));
    System.out.println("Label width before layouted: " + label.getWidth());
    primaryStage.setScene(new Scene(new StackPane(label), 300, 250));
    primaryStage.show();
    System.out.println("Label width after layouted: " + label.getWidth());

The label textProperty string width is: 272.70312
Label width before layouted: 0.0
Label width after layouted: 273.0

, . , .

+2

, , : , , .

+1

:

func foo()
{
    label.layout();

    Platform.runLater(()->
    {
        System.out.println(label.getWidth());
    });
}
+1

All Articles