4

I have the following table:

enter image description here

What I'm wanting to do is extend the column to the size of the window without displaying the extra lines where there is no column (the cells to the right of the item column). I'm somewhat familiar with most of SWT, but these tables are still confusing me.

I want to be able to select an item by clicking anywhere on the row.

Here is the simple UI code I used to make the above screen cap:

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();
}
Jason M.
  • 692
  • 2
  • 8
  • 24
  • It's so much easier to use a widget that was made for this, like a `List`. – Edward Thomson Sep 03 '14 at 21:20
  • Out of context a list might be better, but this question is specifically about tables. – Jason M. Sep 03 '14 at 23:58
  • That's fair. (You can't use cell editors in a `List`, I don't think.) I have done something similar to this with a resize listener but I recall it being prickly. – Edward Thomson Sep 04 '14 at 01:42
  • See [org.eclipse.jface.layout.TableColumnLayout](http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fjface%2Flayout%2FTableColumnLayout.html) class. I'll come back with more elaborate answer later today. – Basilevs Sep 04 '14 at 03:24
  • There is a code snippet [here](http://git.eclipse.org/c/platform/eclipse.platform.swt.git/tree/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet77.java) that should be helpful. – Baz Sep 04 '14 at 08:07
  • possible duplicate of [SWT - table layout - Resize the column of a table to fill all the available space](http://stackoverflow.com/questions/9211106/swt-table-layout-resize-the-column-of-a-table-to-fill-all-the-available-spac) – Basilevs Sep 04 '14 at 14:42

1 Answers1

5

Make your Table to be the only child of its parent and create a column with following:

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));
    }
}

If you experience a strange, growing resize behaviour of table, replace TableColumnLayout with:

/** 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);
    }
}
Community
  • 1
  • 1
Basilevs
  • 22,440
  • 15
  • 57
  • 102