bwright
bwright

Reputation: 936

Resizing label to fit content

I am creating a widget, which will display a text followed by a progress bar. For this I create a Composite

container = new Composite(parent, SWT.BORDER);
container.setLayoutData(layoutData);
container.setLayout(new GridLayout(1, true));

To this I add a Label

messageText = new Label(container, SWT.WRAP | SWT.BORDER | SWT.CENTER);
messageText.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));

followed by a composite holding the progress bar:

  final Composite progressContainer = new Composite(container, SWT.BORDER);
progressContainer
    .setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

This is the result:
enter image description here

What I would expect is the label to grow as to be able to contain the full text. I have been trying to follow the instructions from this post however, I must be missing something as I am not able to achieve the desired behavior.

Thanks for the input.

Upvotes: 1

Views: 583

Answers (1)

greg-449
greg-449

Reputation: 111142

The GridData you have specified uses all the available horizontal space but the vertical space only uses the initial size calculated for the control.

To use all available vertical space use:

messageText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

Note that you are also specifying that the progressContainer should also grab all available space - this is probably not what you want so you may need to change that as well. Possibly:

progressContainer.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));

If you want the message text to resize when you change the text you need to call

container.layout(true);

after setting the new text to force the sizes to be recalculated. Use your original GridData values.

Upvotes: 2

Related Questions