Reputation: 99
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
Upvotes: 6
Views: 17713
Reputation: 61
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.
Upvotes: 5
Reputation: 19
You can use Scene Builder instead for making the UI, it is super easy. There you can easily use "Java fx text area" and everything. all you have to do is to import the jfoenix library and you are ready,and that too without coding.
Upvotes: 1
Reputation: 129
Try setStyle(-fx-padding: 0 10 0 10).
so in this case, 0 - top 10 - right 0 - bottom 10 - left padding.
You can replace 10 with your required value. It simply fits your text in the text box with a padding/space of 10. Had a similar problem. This thing worked for me.
Upvotes: 0
Reputation: 319
Apply the style to the text child:
.text-area *.text {
-fx-text-alignment: center;
}
Upvotes: 7
Reputation: 966
I don't know why you want to use a textArea, but if you just want to display some text you should use a TextField.
Or else to display text use:
Text text = new Text("text");
But i don't know what exactly it is that you want to do.
Upvotes: -5