2
votes

I seem to be getting confused with how Containers calculate their height compared to the components in the container.

I have a Container with a TableLayout that has 1 row and 2 columns. In both cells there are different TextAreas.

TableLayout tl = new TableLayout(1, 2);
Container container = new Container(tl);
TextArea ta1 = new TextArea("Some long text");
TextArea ta2 = new TextArea("Some other long text");
container.add(tl.createConstraint().widthPercentage(50), ta1);
container.add(tl.createConstraint().widthPercentage(50), ta2);

The problem I am having is that the height of container is about double the height of the two TextAreas which leaves a big, unneeded, gap under the TextAreas.

I have tried overriding the calcPreferredSize method which seems to work with any Dimension I put in. The issue is I don't know what the height of the two TextAreas will be.

What am I doing wrong?

Thanks.

1

1 Answers

1
votes

This is an inherent reflow problem. Text area doesn't "know" how much space it will be given so it requests a specific preferred size from the parent layout.

The parent layout uses its own logic which has some relation to preferred size but also some relation to your requests (e.g. 50% width, 2 columns). It gives the text area the space it "thinks" would work. If it doesn't the text area tries to resize once but that can work our inaccurately.

Some frameworks e.g. HTML support a concept of reflow which constantly lays out everything. That's generally why HTML is slower than most native UI's. Reflow seems neat but it's often hard to detect when it's being taxed to "destroy your CPU".

A common trick to avoid this is to determine the rows/columns of a text area and disable the grow with content option of the text area. That makes the component more deterministic in size and also slightly boosts performance. However, it can lead to undesired effects such as text being cropped.