Daniel Peñalba
Daniel Peñalba

Reputation: 31847

SWT label size is not correctly updated

I'm new in Java/SWT. I'm experiencing some troubles using a SWT label.

When I update the text on the label, its size is not correctly updated (the label is cut, respecting the original size). However, if I perform a very small resize in my dialog, the size is updated correctly.

Basically, I create the label with a default text and then, when I load data I update the label with the real text, that is bigger than the original one.

I tried calling label.update() and label.redraw() without luck.

Upvotes: 3

Views: 7121

Answers (3)

Ayman Salah
Ayman Salah

Reputation: 1089

Use of this method is discouraged since it is the least-efficient way to trigger a layout. The use of layout(true) discards all cached layout information, even from controls which have not changed. It is much more efficient to invoke Control.requestLayout() on every control which has changed in the layout than it is to invoke this method on the layout itself.

Based on the documentation of getParent().layout(), you should call requestLayout() on the control itself not its parent as @kingargyle said.

What I always did was label.requestLayout() and it worked flawlessly.

Upvotes: 1

kingargyle
kingargyle

Reputation: 1239

I know this is old, but in order to not lose any LayoutData settings that may be set on the controls. You should call getParent().requestLayout(). The documentation specifically discourages the user of getParent().layout() which loses all the cached Data settings on the controls.

Upvotes: 2

Alexey Romanov
Alexey Romanov

Reputation: 170723

Try to call parent.layout(), where parent is the Composite which contains your label. Also see Understanding Layouts in SWT.

Upvotes: 12

Related Questions