Varusal
Varusal

Reputation: 15

Java - Table won't resize

Hello i'm a beginner in java and have a problem with the sizing of my table.

On the first picture you can see the sizing is very good, but when I click on the button the size of the table will shrink picture two).

Thanks for your help :)

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

Upvotes: 1

Views: 78

Answers (1)

Loris Securo
Loris Securo

Reputation: 7638

If you want the columns to auto size based on the content, then use pack() on the columns, not the table:

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

If you want the table to use the additional vertical space then set its gridData accordingly:

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

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

Upvotes: 2

Related Questions