I want to display a text inside a Java FX Text area.
My code is:
TextArea txt = new TextArea();
txt.setEditable(false);
txt.setStyle("-fx-font-alignment: center");
txt.setText(text);
But I'm not able to set a text alignment (inside the TextArea
)
I tried this:
txt.setStyle("-fx-font-alignment: center");
But it didn't work
TextAlignment class is a part of JavaFX. The TextAlignment class represents the horizontal text alignment. TextAlignment class inherits Enum class. Returns the text Alignment of specified name.
Use a layout pane that can center the label, and let the label be its "preferred size" (i.e. just big enough to hold the text), or. Make the label fill the entire width of its container, and set its alignment property to center.
TextField class is a part of JavaFX package. It is a component that allows the user to enter a line of unformatted text, it does not allow multi-line input it only allows the user to enter a single line of text. The text can then be used as per requirement.
Apply the style to the text child:
.text-area *.text {
-fx-text-alignment: center;
}
I found a solution for centering horizontally the content of textarea only using CSS.
i have used a class in css:
.centeredTextArea .scroll-pane .content .text {
-fx-text-alignment: center;
}
and after i have added
styleClass="centeredTextArea"
to my textArea control.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With