How to get label.getWidth() in javafx
Asked Answered
R

5

5

always when i try to get the width of an Array in Java it just returns 0 and i dont know why. Can somebody explain to me how it is done right?

                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 got an idea what i have to 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 didnt worked - can you explain me more precisely what i need to do?

Revolting answered 12/1, 2014 at 11:22 Comment(0)
T
11

To get the width you need to call prefWidth(-1) and prefHeight(-1) the layout bounds are only set once the control is layouted through resizeRelocate

To get the correct width before the stage is shown you also need to call impl_processCSS(true) which is an NONE public API but there's nothing better at the moment 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));
Treacle answered 12/1, 2014 at 14:21 Comment(3)
*edited question - thank you for the help but i have some problems to understand what exacltly i need to do.Revolting
Thanks, I didn't need the deprecated .impl_processCSS() call however prefWidth(-1) gave me the correct width value whereas getWidth() didn't. CheersKilocalorie
You can use label.applyCss(); instead of l.impl_processCSS(true); It works for me with l.prefWidth(-1). Thanks!Kanara
O
4

Another approach is to calculate the string width of the label's textProperty using Fontloader.computeStringWidth(text, font) which would output the pixel width of the label's textProperty, and is considerably similar to getting the label's width when the label is layouted at the later part.

For an instance:

    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's width is: " + fontLoader.computeStringWidth(label.getText(), label.getFont()));

The output is:

The label's width is: 272.70312

To test:

    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's textProperty string width is: " + fontLoader.computeStringWidth(label.getText(), label.getFont()));
    System.out.println("Label's width before layouted: " + label.getWidth());
    primaryStage.setScene(new Scene(new StackPane(label), 300, 250));
    primaryStage.show();
    System.out.println("Label's width after layouted: " + label.getWidth());

Here's the output

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

Comparably, they are the same. Hope this helps.

Ophthalmia answered 14/12, 2014 at 17:24 Comment(1)
This is accurate with width. However, for some reason, it is not accurate with height. Any ideas?Jackass
F
1

I don't think the width will be calculated until the Label is shown: add it to a Parent that is visible and you should get a non-zero result.

Fashionable answered 12/1, 2014 at 11:27 Comment(5)
thank you, but is there a way that i can get the width before i show them because this is part of a loop and i generate over 100 label's before they are shown.Revolting
@Revolting I don't think so - imagine that you place your label in an AnchorPane with constraints that the label fills the whole region - the size will depend on the size of the AnchorPane, which will probably depend on the size of its container etc. So the actual visible size of your Label depends on how and where it will be shown, which is not available until it becomes visible.Fashionable
You should explain the reason why you need to know the size - there may be a better way.Fashionable
the reason is this: $label.relocate((x1 + x2) / 2 - label.getWidth() / 2, (y1 + y2) / 2);$ i want to place my label centered on the x-coordinate.Revolting
You could do that once the Label is visible. Alternatively, you could maybe use a more appropriate container. For example, can't you use a GridPane and center the labels in the cells of the grids with constraints instead of calculating the positions manually?Fashionable
T
1

You can try the following:

func foo()
{
    label.layout();

    Platform.runLater(()->
    {
        System.out.println(label.getWidth());
    });
}
Thorazine answered 2/8, 2017 at 17:50 Comment(1)
This works. The only unfortunate part is that it updates after it is shown, so you can see anything in the runLater() apply as it happens.Jackass
S
0

You can override layoutChildren() method for the node that contains your labels like:

override fun layoutChildren() {
    super.layoutChildren()
    //after that all label's bounds will be calculated
    label.boundsInParent.width
}
Stagemanage answered 16/7, 2024 at 6:11 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.