SWT - computingSize for Multi-line textfield inside ScrolledComposite

妖精的绣舞 提交于 2019-12-23 12:37:25

问题


I have a composite(innerComposite) within a ScrolledComposite(sc). At runtime, additional UI components can be added. So I'm using the code below to set the minimum size of the sc to enable scrolling, in case that the additional UI components "overflow" the sc.

sc.setMinSize(
    innerComposite.computeSize(
        innerComposite.getSize().x, SWT.DEFAULT
    )
);

One issue is with the SWT Multi-Line textfield inside this sc/innerComposite.

textBox = new org.eclipse.swt.widgets.Text(parent, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);

When I enter a long text into this multi-line textfield, the vertical scrollbar will appear, (which is what I wanted!). However, when I call the code above to recompute the size and setting the sc.setMinSize()...the multi-line textfield's height will expand to fit the length of the text entered. This is not what I wanted. I want this height to remain on with the scrollbar and not resize to fit the text entered.

I know computeSize will cast its children to resize to the preferred size. I don't want this to happen to the multiline textfield as it has the capability of scrolling the vertical bar.

How can I prevent this from happening?


回答1:


An alternative way of solving your problem is writing your own custom layout and setting that to your inner composite. This way you can control exactly how the controls will appear.




回答2:


The solution is to use GridLayout in the content Composite. This allows you to set a GridData as layoutData on the text which contains both a wHint+hHint (which is used when doing computeSize() on the composite ) but still activate verticalGrab which will attribute any additional space to this composite.

In other words: the preferred size of the GridLayouted Composite uses the hHint/wHint from the GridData's of its children. This allows you to set the preferred size of you Text while still expanding the text using GridData.verticalGrab if extra space is available.




回答3:


i solved this by setting the width/height!



来源:https://stackoverflow.com/questions/12834482/swt-computingsize-for-multi-line-textfield-inside-scrolledcomposite

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