Search code examples
javaswteclipse-rcprcp

How to add a group out of a composite in SWT?


I have a filter composite in parent. My example image is here:

enter image description here

I want to add a group out of this filter composite. My example code is below:

// create parent layout
    GridLayout parentLayout = new GridLayout(1, true);
    parent.setLayout(parentLayout);         

    // design filter composite layout
    Group grp = new Group(parent, SWT.NONE);

    RowLayout grdLay = new RowLayout();

    GridData grdData = new GridData();

    grp.setLayoutData(grdData);

    grp.setLayout(grdLay);

    Composite grpComp = new Composite(grp,SWT.NONE);

    GridData gdData = new GridData();       

    RowLayout grplayout = new RowLayout();

    grpComp.setLayout(grplayout);

    grpComp.setLayoutData(gdData);

    Composite filterComposite = new Composite(grpComp, SWT.NONE);
    GridData gd_filterComposite = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
    gd_filterComposite.widthHint = 1611;
    gd_filterComposite.heightHint = 42;
    filterComposite.setLayoutData(gd_filterComposite);

    RowLayout filterCompositeLayout = new RowLayout();
    filterCompositeLayout.type = SWT.VERTICAL;
    filterCompositeLayout.marginHeight = 5;
    filterCompositeLayout.center = true;
    filterCompositeLayout.fill = true;
    filterCompositeLayout.justify = true;
    filterCompositeLayout.wrap = true;
    filterComposite.setLayout(filterCompositeLayout);

    filterCompositeLayout.spacing = 20;

But I had an error. My error image is here:

enter image description here

How can I achive this? Can anybody give some advice? Thank you.


Solution

  • For controls which are part of a RowLayout the layout data must be RowData.

    So

    RowData gdData = new RowData();
    

    because grpComp is part of the RowLayout set on grp

    and similarly the filterComposite layout data should be RowData.

    You must always match a control's layout data to the layout of the Composite which contains the the control.