GridLayout彼此之间有两个表

时间:2014-12-17 19:54:09

标签: java swt grid-layout

我想创建一个包含两个表的对话框。这两个表必须共享垂直对齐的空间。如果表格中有许多元素,则必须显示滚动条。

@Override
protected Control createDialogArea(Composite parent) {
    createSecondDialog();
    GridLayoutFactory layout = GridLayoutFactory.fillDefaults().numColumns(
            1);
    GridDataFactory grid = GridDataFactory.fillDefaults().grab(true, false);

    Composite composite = (Composite) super.createDialogArea(parent);
    layout.applyTo(composite);

    composite.setBackground(Display.getDefault().getSystemColor(
            SWT.COLOR_CYAN));

    Composite section = new Composite(composite, SWT.NONE);
    layout.applyTo(section);
    grid.applyTo(section);

    createTable(section);
    createTable(section);

    return composite;
}

private TableViewer createTable(Composite area) {
    CheckboxTableViewer table = CheckboxTableViewer.newCheckList(area,
            SWT.READ_ONLY | SWT.FULL_SELECTION | SWT.MULTI | SWT.V_SCROLL);

    table.getTable().setBackground(
            Display.getDefault().getSystemColor(SWT.COLOR_DARK_GRAY));
    GridDataFactory.fillDefaults().grab(true, false).
            .applyTo(table.getTable());

    table.setLabelProvider(new LabelProvider() {
        @Override
        public String getText(Object element) {
            if (element instanceof String) {
                return (String) element;
            }
            return "Test";
        }
    });

    table.setContentProvider(ArrayContentProvider.getInstance());
    Collection<String> input = new ArrayList<String>();
    fillArrayList(input);
    table.setInput(input);
    table.getTable().setHeaderVisible(true);
    table.getTable().setLinesVisible(true);

    Button copyButton = new Button(area, SWT.PUSH);
    GridDataFactory.fillDefaults().align(SWT.END, SWT.FILL)
            .applyTo(copyButton);
    copyButton.setText("Instant Copy");
    copyButton.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent selectionevent) {
            // TODO Auto-generated method stub

        }
    });

    return table;
}

我对此感到不满,并且不知道实现我的要求的答案。

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您可以通过覆盖方法Dialog来限制getInitialSize()的高度。然后只需将GridLayout与一列一起使用,并为两个表设置GridData,告诉他们向两个方向展开。这是一个例子:

public MyDialog(Shell parentShell)
{
    super(parentShell);
}

@Override
protected Control createDialogArea(Composite parent)
{
    Composite container = (Composite) super.createDialogArea(parent);
    container.setLayout(new GridLayout(1, false));

    createTable(container);
    createTable(container);

    return container;
}

private void createTable(Composite parent)
{
    Table table = new Table(parent, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
    table.setHeaderVisible(true);

    TableColumn column = new TableColumn(table, SWT.NONE);
    column.setText("Column");

    for (int i = 0; i < 500; i++)
    {
        new TableItem(table, SWT.NONE).setText("Item " + i);
    }

    column.pack();

    table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
}

@Override
protected boolean isResizable()
{
    return true;
}

@Override
protected void configureShell(Shell newShell)
{
    super.configureShell(newShell);
    newShell.setText("StackOverflow");
}

@Override
protected Point getInitialSize()
{
    return new Point(450, 300);
}

public static void main(String[] args)
{
    new MyDialog(new Shell()).open();
}

看起来像这样:

enter image description here