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?
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
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:
The output is:
To test:
Here's the output
Comparably, they are the same. Hope this helps.
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.
You can try the following: