Search code examples
javaswteclipse-rcpscrollbar

Eclipse SWT fixed table height, show scrollbar


I have the following problem...
I have a Dialog, that contains a TabFolder which itself contains a TabItem with a Table inside. The Table has a huge amount of items, causing it to expand not only it's own height, but also the height of the entire shell (which is the main problem here). I don't want that, I want the table to have a fixed height with a vertical scrollbar. I put together a simplified code example, that shows my problem:

public class Main {
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
        Composite contentComposite = new Composite(shell, SWT.NONE);
        contentComposite.setLayout(new GridLayout(4, false));

        Composite tableComposite = new Composite(contentComposite, SWT.NONE);
        tableComposite.setLayout(new GridLayout());
        tableComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

        TabFolder tabFolder = new TabFolder(tableComposite, SWT.NONE);

        TabItem tabItem1 = new TabItem(tabFolder, SWT.NONE);
        tabItem1.setText("Item 1");

        TabItem tabItem2 = new TabItem(tabFolder, SWT.NONE);
        tabItem2.setText("Item 2");

        Table table = new Table(tabFolder, SWT.BORDER | SWT.V_SCROLL);
        table.setLinesVisible(true);
        table.setHeaderVisible(true);

        String[] titles = { " ", "C", "!", "Description", "Resource", "In Folder", "Location" };
        for (int i = 0; i < titles.length; i++) {
            TableColumn column = new TableColumn(table, SWT.NONE);
            column.setText(titles[i]);
        }

        for (int i = 0; i < 100; i++) {
            TableItem item = new TableItem(table, SWT.NONE);
            item.setText(0, "x");
            item.setText(1, "y");
            item.setText(2, "!");
            item.setText(3, "this stuff behaves the way I expect");
            item.setText(4, "almost everywhere");
            item.setText(5, "some.folder");
            item.setText(6, "line " + i + " in nowhere");
        }

        for (int i=0; i<titles.length; i++) {
            table.getColumn (i).pack ();
        }

        table.setSize(200, 200);
        tabItem1.setControl(table);

        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}

Setting the table size doesn't help, since this always happens after it is filled and so changing the size of the everything else.


Solution

  • I think you need to decide what the desired height of the table should be. For example 20 rows. Then you can specify a layout hint (in pixels). If, like in your case, the table has a parent with a GridLayout, you can set the table's GridData like this:

    GridData gridData = new GridData( SWT.FILL, SWT.FILL, true, true );
    gridData.heightHint = 20 * table.getItemHeight();
    table.setLayoutData( gridData );
    

    Setting the size of the table explicitly (table.setSize(200, 200) in your snippet) has no effect if its parent has a layout that manages the size and location of the table.