Reputation: 106
I'm using a GridLayout...
On designing the look, I had a SWT table that is supposed to fit nicely (aesthetically) before I put in data.
For the data to show, I have to pack() each column
for(int i = 0; i < table.getColumnCount(); i++){ table.getColumn(i).pack(); }
but doing so will expand the table's width, sometimes pushing other controls away or just making the table wider than it should be.
I need to keep this shell the same size as it is, so changing the shell's size is not an option.
How do I stop the table from expanding with the column widths?
Note: I WANT the scroll bars to come out instead. But just putting in the option to do so
new Table(myShell, SWT.V_SCROLL | SWT.H_SCROLL)
didn't help my problem
Upvotes: 0
Views: 1421
Reputation: 36894
You can use GridData.widthHint
and GridData.heightHint
to force a certain width/height of the Table
. This way, it will not increase/decrease in size if it's content changes:
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new GridLayout(1, false));
final Table table = new Table(shell, SWT.V_SCROLL | SWT.H_SCROLL);
table.setHeaderVisible(true);
final GridData data = new GridData(SWT.BEGINNING, SWT.BEGINNING, false, false);
data.widthHint = 200;
data.heightHint = 200;
table.setLayoutData(data);
for(int i = 0; i < 5; i++)
{
TableColumn col = new TableColumn(table, SWT.NONE);
col.setText("C" + i);
col.pack();
}
Button button = new Button(shell, SWT.PUSH);
button.setText("Fill table");
button.addListener(SWT.Selection, new Listener()
{
@Override
public void handleEvent(Event arg0)
{
for(int i = 0; i < 100; i++)
{
TableItem item = new TableItem(table, SWT.NONE);
for(int j = 0; j < table.getColumnCount(); j++)
{
item.setText(j, "Item " + i + "," + j);
}
}
for(int j = 0; j < table.getColumnCount(); j++)
{
table.getColumn(j).pack();
}
}
});
shell.pack();
shell.setSize(400, 300);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
Before adding the data:
After adding the data:
Keep in mind that it isn't good practice to force certain sizes on components. In your case, where the Shell
itself is fixed to a certain size, I guess it's ok to do so.
Upvotes: 1