TextArea resize to match the height of text

Hello,

I’m programmatically creating TextArea in a Form with height AUTO that is inside ScrollBox with height 100%.
Something like this:
TextArea aTextArea = uiComponents.create(TextArea.class);
aTextArea.setEditable(false);
aTextArea.setIcon(WARNING_ICON);
aTextArea.setStyleName(“warning”);
aTextArea.setValue(somestring);

somestring contains text that has line breaks. I can count the number of rows with for example
somestring.split("[\n|\r]").length

With that I tried to SetRows(), however the height of the TextArea is not changing, it picks some default height value from somewhere. Experimented with various settings for height too.

I would need help with that so I can display the text in a visual component that has no vertical scrollbar, but adapts to the height of the text being contained within. It can be RichTextArea or ResizableTextArea too as long as I can setEditable(false)
It may be that there was already a discussion about this, but unfortunately, I can’t find it.

Kind regards,
Mladen

I have solved this by adding a listener to calculate the number of rows and setting it.


aTextArea.addValueChangeListener(e → {
int rows = somestring.split("\n|\r").length;
aTextArea.setRows(rows);
});
aTextArea.setValue(somestring);

1 Like