1

I am trying to align the exact center of a Text object in JavaFX, and have it sit in the exact center of the JavaFX window. This part works fine. Whenever I add in another Text object that reads 'SETTINGS', I use the same formula, but I change the y-axis by 10 pixels. For some reason, the second text aligns to the right of the other text, while keeping the changed y-axis. Why is this?

This is my formula: (widthOfWindow / 2) - (widthOfTextObject / 2)

This should give me the exact center of the the x-axis, right? I simply change it to fit the y-axis.

EDIT1: This link is a picture of the method I'm using, as well as the product.

  • The width of your text objects is the same. It's probably greater than the width of the rendered string. – Mad Physicist Dec 11 '17 at 00:44
  • I bet there's a setting to make your text object fit the size of the actual text it contains. – Mad Physicist Dec 11 '17 at 00:45
  • 2
    Or perhaps you are accidentally reusing the dims of the second one when computing for the first? Either way, can't tell without seeing your code. – Mad Physicist Dec 11 '17 at 00:47
  • Possible duplicate of [*JavaFX—centered Text in Scene*](https://stackoverflow.com/q/37541279/230513). – trashgod Dec 11 '17 at 01:05
  • 4
    Why compute the coordinates yourself? Use a [layout pane](https://docs.oracle.com/javase/8/javafx/layout-tutorial/builtin_layouts.htm#JFXLY102) that lays things out the way you want – James_D Dec 11 '17 at 01:37
  • Related: [How can I best place a Label centered in a Shape?](https://stackoverflow.com/questions/23258605/javafx-how-can-i-best-place-a-label-centered-in-a-shape) and [how to put a text into a circle object to display it from circle's center?](https://stackoverflow.com/questions/17437411/how-to-put-a-text-into-a-circle-object-to-display-it-from-circles-center), a trick in the last case is to use [`TextBoundsType.VISUAL`](https://docs.oracle.com/javase/8/javafx/api/javafx/scene/text/TextBoundsType.html#VISUAL) – jewelsea Dec 18 '17 at 23:57
  • The problem is that I'm trying to place the center of the Text object at the center of the screen. In other words, half the Text object would be placed on one side of the median line, and the other half on the other side. Would TextBoundsType.VISUAL negate the width of each character, and simply refer to the entire width of the Text Object? –  Dec 19 '17 at 00:02
  • The text bounds type, just changes how the bounds returned by functions like `text.getBoundsInLocal()` is calculated. Read the doc for what it means. Logical bounds will take into account "the potential ascent and descent of text using the font at its specified size" (for instance the downstroke of a y even if your text doesn't have a y), but visual bounds will not. None of that has anything to do with a median line, which I guess is something you are calculating independently. – jewelsea Dec 19 '17 at 00:19

1 Answers1

0

It turns out that my font doesn't have the same width across all it's characters, preventing me from accomplishing it. I've picked another font to use, and it works brilliantly. Thanks guys!!