May 3, 2008

Single column TableViewer and TableColumnLayout

If you have a single column TableViewer (which is commonly used because ListViewer won't show you the images), the single coloumn won't take the entire space. For example if you run the TableViewer snippet, the output is like this:





To get rid of the other spurious coloum that appears on the right, you have to use the TableColumnLayout. Modifying the code to use that:



public Snippet001TableViewer(Shell shell) {



Composite tableComposite = new Composite(shell, SWT.NONE);

final TableViewer v = new TableViewer(tableComposite);

v.setLabelProvider(new LabelProvider());

v.setContentProvider(new MyContentProvider());

MyModel[] model = createModel();

v.setInput(model);

v.getTable().setLinesVisible(true);



TableColumn singleColumn = new TableColumn(v.getTable(), SWT.NONE);

TableColumnLayout tableColumnLayout = new TableColumnLayout();

tableColumnLayout.setColumnData(singleColumn, new ColumnWeightData(100));

tableComposite.setLayout(tableColumnLayout);



}



Remember, the TableColumnLayout should be applied on the composite that holds the Table. And the composite should contain only the table and nothing else. Here is the result:



0 comments: