In a custom JavaFX UI Control, I want to place some text in the corners of the Control. Here is the source code from my Skin class:
double width = control.getWidth();
double height = control.getHeight();
Text test1Text = new Text(0, 0, "top left");
Text test2Text = new Text(0, height-1, "bottom left");
Text test3Text = new Text("top right");
test3Text.relocate(width - test3Text.getLayoutBounds().getWidth(), 0);
Text test4Text = new Text("bottom right");
test4Text.relocate(width - test4Text.getLayoutBounds().getWidth(), height-1);
Unfortunately, it seems to make a difference, whether I construct a text at a given co-ordinate or whether I construct it without co-ordinates and relocate it in place afterwards:
Any ideas for this strange behaviour?
Text
is being drawn from (0,0) position to the right and up. E.g. if you create new Text("Hello")
and ask for it bounds you will see that they have negative vertical coordinates [minX:0.0, minY:-12.94921875]
The reason IMHO is next: Text
is being drawn in Controls and they care more about baseline of the text. Imagine 2 buttons with text "water" and "Water" -- you would really expect them to be aligned by the baseline rather then top-left corner:
relocate()
method from the other side works with a regular Node
s and operates layout which always calculated for top-left corner.