Search code examples
javatextjavafxalignmentlabel

Centering multiple line text in JavaFX label


I'm struggling to center my multiple line Label in JavaFX.
I splitted each line using \n, and I can't center the whole text. I've been trying to use label.setAlignment(Pos.CENTER)but it just doesn't work.

Here is what I've got:
xxxxxxxxx
xxxxxx......
xxxx..........

this is what I need:
xxxxxxxxx
...xxxxxx...
.....xxxx.....

I know it has to be a better solution than filling labels with unnecessery spaces.


Solution

  • Don't use setAlignment. You don't want to just align the Label itself, but the text within it. Try setTextAlignment instead.