SWT - computingSize for Multi-line textfield inside ScrolledComposite

1.7k views Asked by At

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?

3

There are 3 answers

0
parasietje On

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.

0
rperetti On

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.

1
humansg On

i solved this by setting the width/height!