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?
The solution is to use
GridLayout
in the contentComposite
. This allows you to set aGridData
aslayoutData
on the text which contains both awHint
+hHint
(which is used when doingcomputeSize()
on the composite ) but still activateverticalGrab
which will attribute any additional space to this composite.In other words: the preferred size of the
GridLayout
edComposite
uses thehHint
/wHint
from theGridData
's of its children. This allows you to set the preferred size of youText
while still expanding the text usingGridData.verticalGrab
if extra space is available.