Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java FX TextArea Alignment

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

like image 247
user2410265 Avatar asked Dec 04 '13 17:12

user2410265


People also ask

How to Align text in Java fx?

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.

How do I center a label in JavaFX?

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.

What is JavaFX text?

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.


2 Answers

Apply the style to the text child:

.text-area *.text {
    -fx-text-alignment: center;
}
like image 70
Martynas Avatar answered Sep 25 '22 23:09

Martynas


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.

like image 20
siouxes Avatar answered Sep 23 '22 23:09

siouxes