0
votes

I need to change the anchorpoint for a label in JavaFX. I describe the anchor as the point that is picked to translate the underlying node. As default the anchorpoint seems to be the upper left edge.

I tried to solve the problem with an additional translation as described below:

double x_relocate = label.getBoundsInParent().getWidth();
label.setTranslateX(label.getTranslateX() - x_relocate);

double y_relocate = label.getBoundsInParent().getHeight();
label.setTranslateY(des.getTranslateY() + y_relocate);

The code should translate the label so that it acts like the anchorpoint would be in the lower right corner. This does not not work because at the point I do execute the code my bounding box for the label is [minX:0.0, minY:0.0, minZ:0.0, width:-1.0, height:-1.0, depth:0.0, maxX:-1.0, maxY:-1.0, maxZ:0.0].

Does anyone know a solution for this?

EDIT: Added a picture for better unterstanding

Bild hochladen

1
Try those codes after the stage.show() command. - Uluk Biy
still, it is very odd to change the anchorage by using a translation. Is there no other way to do this? - gimba

1 Answers

0
votes

Okay, I solved the problem myself. Here is what I do:

  1. give the labels a width and height that has enough space for the longest text
  2. relocate the labels in every quadrant.
  3. Set the text alignment in every quadrant

Example (width: 100, height: 15):

  • East to South: nothing to do here because the left upper edge is used by default South to West
  • South to East: relocate the label 100 points to the right to have the upper right edge stick to the circle + set the text alignment to UPPER_RIGHT

I hope that help someone sometime.