Search code examples
javaswteclipse-rcpjface

How to resize children inside a dialog, when dialog is resized


I have a composite in a dialog. I want to resize the dialog and when the dialog is resizing i want to update the composite accordingly.

How do I set the resizing of the composite also?

Below is my code snippet :

protected Control createDialogArea(Composite parent) {
Composite container = (Composite) super.createDialogArea(parent);
final GridLayout gridLayout = new GridLayout();
container.setLayout(gridLayout);

final Composite composite = new Composite(container, SWT.NONE);
final GridData gridData = new GridData(GridData.FILL, GridData.FILL, false, 
                                                                 false);
gridData.heightHint = 300;
gridData.widthHint = 600;
composite.setLayoutData(gridData);

I have a TableViewer inside this Composite.

Please let me know how to handle this one.


Solution

  • Be sure to call setLayout on every Composite you create.

    If you use GridLayout for the layout of a composite use GridData which specifies SWT.FILL and 'grab extra space`:

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

    will make the control use all the horizontal space.

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

    will make the control take all available space in both directions.

    Note: You must create a new GridData for each control - do not try to share them between controls (the same applies to layouts).

    To make a dialog resizeable in the first place override isResizable:

    @Override
    protected boolean isResizable()
    {
      return true;
    }