I have TextArea
in JavaFX
. I need to align text vertically or make TextArea
to be same size as content. Currently, TextArea
is growing to fill its parent height.
Asked
Active
Viewed 1,125 times
0

Alyona
- 1,682
- 2
- 21
- 44
-
If you will not be editing the texting, use a `Label`. – SedJ601 Mar 28 '18 at 16:11
-
1I need text to be selectable, that's why I went for `TextArea` – Alyona Mar 28 '18 at 16:38
-
If no one posts a solution, you can look at these workarounds: https://stackoverflow.com/questions/44173811/how-to-make-a-javafx-label-selectable/44182371#44182371 https://stackoverflow.com/questions/25572398/how-do-i-create-an-editable-label-in-javafx-2-2 – SedJ601 Mar 28 '18 at 16:44
-
What's about `Wrap Text` ? – Menai Ala Eddine - Aladdin Mar 28 '18 at 19:32
-
Menai Ala Eddine, `Wrap Text` is not aligning text vertically – Alyona Apr 05 '18 at 10:41
1 Answers
0
I sorted it by creating a Label
, placing it behind TextArea
, making it invisible(just in case) and binding TextArea height to label:
@FXML
private TextArea msg;
@FXML
private Label bindingText;
@FXML
private Button button;
@FXML
private void initialize() {
bindingText.textProperty().bind(msg.textProperty());
msg.setText(Word.customTranslate("data_transfer_failed", Locale.getDefault().getLanguage()));
msg.prefHeightProperty().bind(bindingText.heightProperty());
button.setText(Word.customTranslate("close_app", Locale.getDefault().getLanguage()));
}
Get rid of TextArea
borders through styling and center it. This way I got centered text, that can also be selected.

Alyona
- 1,682
- 2
- 21
- 44