【问题标题】:GridLayout two tables among each otherGridLayout 两个表相互之间
【发布时间】:2014-12-18 16:25:17
【问题描述】:

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

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

我正在苦苦挣扎,不知道实现我的要求的答案。

感谢您的帮助!

【问题讨论】:

  • 你的代码,到底是什么样子的?它与您尝试实现的目标有何不同?
  • 桌子的高度不受限制。如果我每张桌子有 500 个项目,我就没有滚动条。桌子走出窗外。我的愿望是获得一个对话框,假设高度为 550 像素,在此窗口中 2 个高度为 250 像素的表格,如果表格中的项目超过表格可以显示的表格,则表格必须获得滚动条。

标签: java swt grid-layout


【解决方案1】:

您可以通过重写方法getInitialSize() 来限制Dialog 的高度。然后只需将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();
}

看起来像这样:

【讨论】:

    猜你喜欢
    • 2014-04-11
    • 1970-01-01
    • 2020-03-15
    • 2017-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-12
    相关资源
    最近更新 更多