Search code examples
javafxjavafx-cssjavafx-textarea

center align textarea in JavaFX with CSS


I know I am supposed to set -fx-text-alignment: center; in order to achieve a center-aligned textarea.

#txtFaContent
{
    -fx-text-alignment: center;
}

with txtFaContent being the ID of my textarea (as well as its variable name inside the controller). But it doesn't have any effect on the text alignment of my textarea (I played with right/left/center; but no success).

Have I missed anything?


Solution

  • Add style class in your TextArea.text:

    TextArea textArea = new TextArea();
    textArea.getStyleClass().add("centered-text-area");
    

    Add .text in your CSS:

    .centered-text-area .text {
      -fx-text-alignment: center;
    }