can I combine SWT GridLayout and FillLayout

泪湿孤枕 提交于 2019-12-04 12:50:27

问题


I have an RCP/SWT application in which I'm trying to construct a view out of existing composites. One is a FillLayout composite, the other uses GridLayout.

I'd to like to end up with a view in which the GridLayout composite is lined up to the left of the FillLayout composite (think vertical banner) and is about 10 percent the width of the entire view, with the existing FillLayout composite comprising the other 90 percent.

I'm not sure if it is possible in SWT to combine layouts, but I'm thinking something like a GridLayout with two columns. Column one would contain the GridLayout widget and column two would contain the FillLayout composite. Can this be done in SWT? If so, what does this look like code-wise?

Thanks-


回答1:


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();




回答2:


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});



来源:https://stackoverflow.com/questions/16846509/can-i-combine-swt-gridlayout-and-filllayout

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