SWT label size is not correctly updated

跟風遠走 提交于 2019-12-07 13:02:55

问题


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.


回答1:


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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/7554922/swt-label-size-is-not-correctly-updated

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!