2014-03-03 59 views
3

我在單元格中使用帶編輯器(文本,組合和檢查)的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(); 
} 
+0

YOu應該使用TableViewer並編輯支持 – Mirco

回答

-2

做這樣的事情: 第一

table.setRedraw(false) ; 

,並在年底做你的工作 設置真的,像這樣:

table.setRedraw(true); 
+1

如何這與計算正確的大小有關? – appcoder

+0

你這樣做,你會發現如何! –

+0

@omidhaghighatgoo如果您確定這會起作用,那麼請解釋您將使用這兩個命令的位置。理想地發佈一個完整的工作示例... – Baz