SWT:GridLayout列范围

时间:2012-08-29 01:36:04

标签: layout swt html composite grid-layout

我已经使用SWT几年了,但我似乎无法想出这个:

我需要将视图拆分为2个“区域”:左和右 所以我使用带有2列GridLayout的Composite来完成它。

在RIGHT复合体中,我有一个固定数量的列(在另一个GridLayout复合体内创建),但在LEFT复合体内我需要创建一个跨越复合限制的DYNAMIC列数...

有没有办法实现这个目标?

我在左侧复合中尝试了一个RowLayout,但这两个不匹配: - \

由于

1 个答案:

答案 0 :(得分:2)

目前尚不清楚你的意思

  1. 跨越复合限制的列
  2. 我在左侧复合材料中尝试了一个RowLayout,但这两个不匹配
  3. 因此,我假设你想要一个动态改变左边复合的gridlayout。请参阅下面的代码,特别是button.addSelectionListener()

    import java.util.Random;
    
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.events.SelectionAdapter;
    import org.eclipse.swt.events.SelectionEvent;
    import org.eclipse.swt.layout.GridData;
    import org.eclipse.swt.layout.GridLayout;
    import org.eclipse.swt.widgets.Button;
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Label;
    import org.eclipse.swt.widgets.Shell;
    
    public class DynamicComposite 
    {
        public static void main(String[] args) {
            new DynamicComposite().start();
        }
    
        private Composite compositeLeft;
        private Composite compositeRight;
        private Random rand;
    
        public void start()
        {
            rand = new Random(System.currentTimeMillis()); 
    
            Display display = new Display();
            Shell shell = new Shell(display);
            shell.setLayout(new GridLayout(2, true));
            shell.setText("Dynamic Columns");
    
            createRightComposite(shell);
            createLeftComposite(shell);
            createButtonComposite(shell);
    
            shell.open();
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch())
                    display.sleep();
            }
            display.dispose();
        }
    
        private void createButtonComposite(Shell shell) 
        {
            Label l = new Label(shell, SWT.SEPARATOR|SWT.HORIZONTAL);
            GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, false);
            gridData.horizontalSpan = 2;
            l.setLayoutData(gridData);
    
            Button button = new Button(shell, SWT.PUSH);
            gridData = new GridData(SWT.CENTER, SWT.CENTER, true, false);
            gridData.horizontalSpan = 2;
            button.setLayoutData(gridData);
            button.setText("Change Columns !!");
    
            button.addSelectionListener(new SelectionAdapter() {
                public void widgetSelected(SelectionEvent e) {
                    if(compositeLeft == null || compositeLeft.isDisposed())
                        return;
    
                    GridLayout layout = (GridLayout)compositeLeft.getLayout();
                    int col = rand.nextInt(7);
                    if(col == 0)
                        col = 1;
                    layout.numColumns = col;
                    //compositeLeft.setLayout(layout);
                    compositeLeft.layout(); // You need to re-layout your composite
                }
            });
        }
    
        private void createRightComposite(Shell shell) 
        {
            compositeLeft = new Composite(shell, SWT.NONE);
            GridLayout gridLayout = new GridLayout(3, true);
            gridLayout.marginWidth = 0;
            gridLayout.marginHeight = 0;
            gridLayout.marginLeft = 0;
            gridLayout.marginRight= 0;
            gridLayout.marginTop = 0;
            gridLayout.marginBottom = 0;
            gridLayout.horizontalSpacing = 0;
    
            gridLayout.verticalSpacing = 0;
            gridLayout.horizontalSpacing = 0;
            compositeLeft.setLayout(gridLayout);
            GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
            compositeLeft.setLayoutData(gridData);
    
            int counter = 17;
            for(int i=0; i<counter; i++)
            {
                Button button = new Button(compositeLeft, SWT.PUSH);
                button.setText("Button " + (i+1));
                GridData bData = new GridData(SWT.FILL, SWT.FILL, true, false);
                button.setLayoutData(bData);
            }
        }
    
        private void createLeftComposite(Shell shell) 
        {
            compositeRight = new Composite(shell, SWT.NONE);
            GridLayout gridLayout = new GridLayout(3, true);
            gridLayout.marginWidth = 0;
            gridLayout.marginHeight = 0;
            gridLayout.marginLeft = 0;
            gridLayout.marginRight= 0;
            gridLayout.marginTop = 0;
            gridLayout.marginBottom = 0;
            gridLayout.horizontalSpacing = 0;
            compositeRight.setLayout(gridLayout);
    
            GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
            compositeRight.setLayoutData(gridData);
    
            int counter = 7;
            for(int i=0; i<counter; i++)
            {
                Button button = new Button(compositeRight, SWT.PUSH);
                button.setText("Button " + (i+1));
                GridData bData = new GridData(SWT.FILL, SWT.FILL, true, false);
                button.setLayoutData(bData);
            }
        }
    }