SWT - calculation for multi-line text field inside ScrolledComposite

I have a composite (innerComposite) in ScrolledComposite (sc). At run time, additional user interface components can be added. Therefore, I use the code below to set the minimum sc size to enable scrolling, in case additional components of the user interface overflow sc.

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

One problem is the SWT Multi-Line text box inside this sc / innerComposite.

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

When I enter long text in this multi-line text box, a vertical scroll bar will appear (this is what I wanted!). However, when I call the code above to recalculate the size and set sc.setMinSize () ... the height of the multi-line text box will expand to fit the length of the entered text. This is not what I wanted. I want this height to stay on the scroll bar and not change according to the text entered.

I know that computeSize will discard children to resize the preferred size . I do not want this to happen with a multi-line text field, since it has the ability to scroll through the vertical panel.

How can I prevent this?

+4
source share
3 answers

An alternative way to solve your problem is to write your own custom layout and install it on your internal composition. This way you can precisely control how the controls are displayed.

+1
source

The solution is to use GridLayout in the Composite content. This allows you to set the GridData as layoutData in the text, which contains wHint + hHint (which is used when computeSize() executed on the composite), but it still activates verticalGrab , which will be the attribute of any extra space for this composite.

In other words: the preferred size of the GridLayout ed Composite uses the hHint / wHint of the GridData its children. This allows you to set your preferred Text size while continuing to expand the text using GridData.verticalGrab if extra space is available.

+1
source

I solved this by setting the width / height!

0
source

Source: https://habr.com/ru/post/1439090/


All Articles