1

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.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
bwright
  • 896
  • 11
  • 29

1 Answers1

2

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.

greg-449
  • 109,219
  • 232
  • 102
  • 145
  • These changes do give the label all the possible space, however this moves the progress bar down to the bottom. I would like the label to take the space it requires, but not more than that. This way the progress bar is always directly beneath the text. – bwright Apr 16 '19 at 07:55
  • Perhaps as a side note, the progressContaienr does not need to fill the space, I simply want it directly below the text – bwright Apr 16 '19 at 08:01
  • Then call `layout` after setting the new text - see updated answer. – greg-449 Apr 16 '19 at 08:34