4

I'm creating a simple user interface with JavaFX, where some labels showing texts are used. I tried to move text to the center of labels with the rule:

-fx-text-alignment: center

yet it doesn't work and text is still located on the left by default. I have also checked that I haven't used any other rules that could override this. Any ideas? Here is the css code:

#subtitle
{
    -fx-font: bold italic 18pt "Arial";
    -fx-text-alignment: center;
    -fx-effect: dropshadow( one-pass-box, black, 8, 0.0, 2, 0 );
    -fx-text-fill: #1ebd1e;
}

Java code:

Label title = new Label("Trees");
title.setId("subtitle");
grid.add(title, 0, 0, 2, 1);

enter image description here

Oscar
  • 1,198
  • 1
  • 13
  • 20
  • Add "-fx-border-color: red;" to see how wide the label is. I think you need to span 2 columns on the row that the label located. See gridpane doc for spanning. – Uluk Biy May 19 '15 at 13:10
  • ... and then apply alignment to that spanned cell of gridpane. – Uluk Biy May 19 '15 at 13:19
  • @Uluk Biy If my understanding is correct, the third parameter of "grid.add(title, 0, 0, 2, 1);" should be able to do the spanning on the row. I also tried "GridPane.setColumnSpan(title, 2);", didn't work. But you are right that the label size is just as big as the text size. – Oscar May 19 '15 at 13:51

2 Answers2

14

I have the same problem.

The text in the label cannot be centered or right aligned, or ... nor in CSS nor in code like mylabel.setTextAlignment(TextAlignment.Center) when the label is larger than the text.

I use instead mylabel.setAlignment(Pos.CENTER) to solve the problem; also works in CSS with -fx-alignment:center;

John Hascall
  • 8,682
  • 4
  • 46
  • 64
maurille
  • 141
  • 1
  • 2
  • A key point about [textAlignment](https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Labeled.html#textAlignmentProperty) is that it: "Specifies the behavior for lines of text *when text is multiline* . . . this setting only affects multiple lines of text relative to the text bounds." My experience has been that, when text has multiple lines, textAlignment works as advertised, if it does not for you, you should create a sample application and report a bug: http://bugreport.java.com – jewelsea Jan 29 '16 at 20:08
  • This should be the accepted answer if you're using a Label that doesn't have multiple lines. – Stardust Jul 08 '17 at 16:58
4

You can solve this problem using the code below:

Label title = new Label("Trees");
title.setId("subtitle");
GridPane.setHalignment(title, HPos.CENTER);
gridpane.add(title, 0, 0, 2, 1);

This is the result I got:

enter image description here

Note: The -fx-text-alignment tag is going to center the text inside the label's borders. If the label's width is exactly the size it needs to fit the text, you have no changes about the alignment, but if you set a width bigger than the text needs you can align the text inside the Label's borders.

henriqueor
  • 639
  • 1
  • 9
  • 31
  • Yes, it works for me as well. And I also tried "GridPane.setConstraints(title, 0, 0, 2, 1, HPos.CENTER, VPos.CENTER);" which can do the same thing. But I'm still confused about the way using css style. How to make the css rule: "-fx-text-alignment: center;" work? – Oscar May 19 '15 at 13:59
  • To use the tag you have to give the Label the same width the GridPane has. Then you can center the text inside the Label using this css tag. – henriqueor May 19 '15 at 14:05
  • If my answer solved your problem, please mark as the correct answer. Tks. – henriqueor May 19 '15 at 14:07