can I combine SWT GridLayout and FillLayout

走远了吗. 提交于 2019-12-03 08:03:07

Start setting a FormLayout to your outer composite. Place two other composites inside it, setting their FormData information to position them as you please. Then set those two composite's layouts (Grid and Fill, as you said).

Here's some code to start. There's an image after it showing what it produces. You might also check out Eclipse's SWT Layouts view.

Shell shell = new Shell();

FillLayout fillLayout = new FillLayout();
fillLayout.marginHeight = 5;
fillLayout.marginWidth = 5;
shell.setLayout( fillLayout );

Composite outer = new Composite( shell, SWT.BORDER );
outer.setBackground( new Color( null, 207, 255, 206 ) ); // Green

FormLayout formLayout = new FormLayout();
formLayout.marginHeight = 5;
formLayout.marginWidth = 5;
formLayout.spacing = 5;
outer.setLayout( formLayout );

Composite innerLeft = new Composite( outer, SWT.BORDER );
innerLeft.setLayout( new GridLayout() );
innerLeft.setBackground( new Color( null, 232, 223, 255 ) ); // Blue

FormData fData = new FormData();
fData.top = new FormAttachment( 0 );
fData.left = new FormAttachment( 0 );
fData.right = new FormAttachment( 10 ); // Locks on 10% of the view
fData.bottom = new FormAttachment( 100 );
innerLeft.setLayoutData( fData );

Composite innerRight = new Composite( outer, SWT.BORDER );
innerRight.setLayout( fillLayout );
innerRight.setBackground( new Color( null, 255, 235, 223 ) ); // Orange

fData = new FormData();
fData.top = new FormAttachment( 0 );
fData.left = new FormAttachment( innerLeft );
fData.right = new FormAttachment( 100 );
fData.bottom = new FormAttachment( 100 );
innerRight.setLayoutData( fData );

shell.open();

You can use the following code as a starting point:

public static void main(String[] args)
{
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(1, false));

    SashForm form = new SashForm(shell, SWT.HORIZONTAL);
    form.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    Composite left = new Composite(form, SWT.BORDER);
    left.setLayout(new GridLayout(3, true));
    left.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    for(int i = 0; i < 9; i++)
    {
        Button button = new Button(left, SWT.PUSH);
        button.setText("Button " + i);
        button.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
    }

    final Composite right = new Composite(form, SWT.BORDER);
    right.setLayout(new GridLayout(1, true));
    right.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    Button fillButton = new Button(right, SWT.PUSH);
    fillButton.setText("Fill");
    fillButton.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    /* Set the width to 80% and 20% */
    form.setWeights(new int[] {4, 1});

    shell.setSize(400, 400);
    shell.open();
    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

It looks like this:


It's basically a SashForm with two parts. The left part is a GridLayout with three columns and the right part is a GridLayout with one column. No need to mix Layouts.

The percentage is set with form.setWeights(new int[] {4, 1});

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!