2014-09-03 60 views
2

我有如下表:格式化SWT表與單個列

enter image description here

什麼,我想要做的是延長列到窗口的大小,而不顯示額外的行那裏沒有列(項目列右側的單元格)。我對SWT的大部分都很熟悉,但這些表格仍令我困惑。

我希望能夠通過點擊行的任何位置來選擇一個項目。

這裏是我用來做上面的屏幕頂蓋的簡單的用戶界面代碼:

public static void main(String[] args) { 
    Display display = new Display(); 
    Shell shell = new Shell(display); 
    shell.setLayout(new GridLayout()); 

    Table table = new Table (shell, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION); 
    table.setLinesVisible (true); 
    table.setHeaderVisible (false); 
    GridData data = new GridData(SWT.FILL, SWT.FILL, true, true); 
    data.heightHint = 200; 
    table.setLayoutData(data); 

    TableColumn column = new TableColumn (table, SWT.NONE); 
    column.setResizable(true); 
    int count = 15; 
    for (int i=0; i<count; i++) { 
     TableItem item = new TableItem (table, SWT.NONE); 
     item.setText (0, "item " + i); 
    } 
    column.pack(); 

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

    while(!shell.isDisposed()) { 
     if (!display.readAndDispatch()) { 
      display.sleep(); 
     } 
    } 
    display.dispose(); 
} 
+0

它是如此更容易使用Wi dget是爲這個做的,就像一個'List'。 – 2014-09-03 21:20:35

+0

從上下文中,列表可能會更好,但這個問題是關於表格的。 – 2014-09-03 23:58:49

+0

這很公平。 (你不能在'List'中使用單元格編輯器,我不這麼認爲)。我用一個調整大小的監聽器做了類似的事情,但我記得它很刺眼。 – 2014-09-04 01:42:45

回答

3

讓你的表是其父母的獨子,並用以下命令來創建一個列:

import org.eclipse.jface.viewers.ColumnWeightData; 
import org.eclipse.swt.widgets.Table; 
import org.eclipse.swt.widgets.TableColumn; 
import org.eclipse.jface.layout.TableColumnLayout; 

public class ColumnHelper { 
    public static void createColumn(Table table) { 
     TableColumnLayout layout = new TableColumnLayout(); 
     table.getParent().setLayout(layout); 
     TableColumn column = new TableColumn (table, SWT.NONE); 
     layout.setColumnData(column, new ColumnWeightData(10)); 
    } 
} 

如果遇到表的strange, growing resize behaviour,以取代TableColumnLayout:

/** Forces table never to take more space than parent has*/ 
public class TableColumnLayout extends org.eclipse.jface.layout.TableColumnLayout { 

    @Override 
    protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) { 
     return new Point(wHint, hHint); 
    } 
} 
+0

[相關解決方案](http://stackoverflow.com/a/9211940/125562) – Basilevs 2014-09-04 14:38:40

+0

感謝您的鏈接,讓它現在工作得很漂亮! – 2014-09-09 20:26:33