can I combine SWT GridLayout and FillLayout

前端 未结 2 1171
深忆病人
深忆病人 2021-02-06 12:43

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

相关标签:
2条回答
  • 2021-02-06 13:26

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

    Code output

    0 讨论(0)
  • 2021-02-06 13:26

    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:

    enter image description here


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

    0 讨论(0)
提交回复
热议问题