Eclipse Search

Loading

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:

5 comments:

  1. Hi Prakash,

    I wanted to know if there is a similar solution to remove the extra column in Nebula Grid?
    I could not find a GridColumnLayout and I also cannot use the TableColumnLayout for Nebula Grid.

    Thanks in advance,
    Asha.
    ReplyDelete
  2. @Asha,
    Never tried the nebula grid, so couldn't comment. Posting the question on the nebula newsgroup might be of help
    ReplyDelete
  3. Hi.. i am using Eclipse 3.2.. TableColumnLayout is available after eclipse 3.3 only.. any suggestions to get rid of the same problem in eclipse 3.2.
    ReplyDelete
  4. @Anonymous,
    I'm not sure of a solution for 3.2 :-(
    ReplyDelete
  5. what about multiple columns?
    I also always have that undesired column.
    I tried the following after reading your post but it just "does nothing" I mean. I still get that extra column.
    any other way?



    private void fixTableColumns(Table table) {
    int totalWidth = 0;
    for (TableColumn col: table.getColumns()) {
    totalWidth += col.getWidth();
    }

    int weightSum = 0;
    TableColumnLayout tableColumnLayout = new TableColumnLayout();

    // traverse all except the last one
    for (TableColumn col: table.getColumns()) {
    int weight = (int) (((double)col.getWidth()/(double)totalWidth)*(double)100);
    tableColumnLayout.setColumnData(col, new ColumnWeightData(weight));

    }

    ((Composite)getSection().getClient()).setLayout(tableColumnLayout);
    }

    btw - the section's client is the same composite i create the TableViewer on.
    ReplyDelete