我在單元格中使用帶編輯器(文本,組合和檢查)的SWT表格,並且想要正確調整文本的列大小。我嘗試了TableColumn的Pack方法,但它似乎不工作。使用編輯器調整SWT表格
下面是示例,我使用
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Table table = new Table(shell, SWT.BORDER | SWT.MULTI);
table.setLinesVisible(true);
for (int i = 0; i < 3; i++)
{
TableColumn column = new TableColumn(table, SWT.NONE);
column.setWidth(100);
}
for (int i = 0; i < 12; i++)
{
new TableItem(table, SWT.NONE);
}
TableItem[] items = table.getItems();
for (int i = 0; i < items.length; i++)
{
TableEditor editor = new TableEditor(table);
CCombo combo = new CCombo(table, SWT.NONE);
combo.setText("CCombo");
combo.add("item 1");
combo.add("A very lengthyyyyyyyyy item 2");
editor.grabHorizontal = true;
editor.setEditor(combo, items[i], 0);
editor = new TableEditor(table);
Text text = new Text(table, SWT.NONE);
text.setText("A very lengthyyyyyyyy text");
editor.grabHorizontal = true;
editor.setEditor(text, items[i], 1);
editor = new TableEditor(table);
final Button button = new Button(table, SWT.CHECK);
button.pack();
editor.minimumWidth = button.getSize().x;
editor.horizontalAlignment = SWT.LEFT;
editor.setEditor(button, items[i], 2);
}
//resize columns
for (TableColumn columm : table.getColumns())
columm.pack();
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
YOu應該使用TableViewer並編輯支持 – Mirco