Java - 表格不会调整大小

时间:2017-08-09 18:55:43

标签: java datagrid resize swt

您好我是java的初学者,我的表格大小有问题。

first picture上你可以看到尺寸非常好,但是当我点击按钮时,桌子的大小会缩小picture two)。

感谢您的帮助:)

protected void createContents() {

    ...

    gridData = new GridData();
    gridData.horizontalAlignment = SWT.FILL;
    gridData.horizontalSpan = 2;
    gridData.grabExcessHorizontalSpace = true;

    table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
    table.setLayoutData(gridData);
    table.setHeaderVisible(true);
    table.setLinesVisible(true);

    TableColumn columnProduct = new TableColumn(table, SWT.NONE);
    columnProduct.setText("Product");
    TableColumn columnKey = new TableColumn(table, SWT.NONE);
    columnKey.setText("Key");

    for (int i = 0; i < 2; i++)
    {
        table.getColumn(i).pack();
    }

    shell.pack();
    shell.open();
}

@Override
public void mouseDown(MouseEvent e) {
    TableItem tItem = new TableItem(table, SWT.FILL);
    String[] data = { txtProduct.getText(), txtKey.getText()};
    tItem.setText(data);
    table.pack();
}

1 个答案:

答案 0 :(得分:2)

如果您希望根据内容自动调整列数,请在列上使用pack(),而不是表格:

for (TableColumn column : table.getColumns()) {
    column.pack();
}

如果您希望表格使用额外的垂直空间,请相应地设置其gridData

...
gridData = new GridData();
gridData.horizontalAlignment = SWT.FILL;
gridData.horizontalSpan = 2;
gridData.grabExcessHorizontalSpace = true;

// add these:
gridData.verticalAlignment = SWT.FILL;
gridData.grabExcessVerticalSpace = true;