oliverleo
oliverleo

Reputation: 77

Make controls resizeable

I want to create a page in Eclipse which has two tree components next to each other and a table below the trees.

The code I wrote does that, but I also want the table and trees to be resizable. When I resize make the table smaller the trees should get bigger.

I tried some different ways which I found on the internet but none did work.

Here is the code I wrote:

@PostConstruct
public void postConstruct(Composite parent, MApplication app) {
            Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayoutData(new GridData(SWT.FILL,SWT.FILL, true, true, 1,1));
    composite.setLayout(new GridLayout(2, true));
    treeLeft = new TreeViewer(composite);
    treeLeft.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));

    treeRight = new TreeViewer(composite);
    treeRight.getTree().setLayoutData(new GridData(GridData.FILL_BOTH));

    Composite tableComposite = new Composite(parent, SWT.NONE);
    tableComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
    tableComposite.setLayout(new GridLayout());
    table = new TableViewer(tableComposite, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
    GridData grid = new GridData(GridData.FILL_BOTH);
    grid.horizontalSpan = 4;
    table.setContentProvider(ArrayContentProvider.getInstance());
    createTableColumns();
    table.getControl().setLayoutData(grid);
    table.getTable().setHeaderVisible(true);
    table.getTable().setLinesVisible(true);

Upvotes: 1

Views: 782

Answers (1)

Baz
Baz

Reputation: 36884

Use a SashForm to be able to resize your components. Here is an example:

public static void main(String[] args)
{
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("StackOverflow");
    shell.setLayout(new GridLayout(1, false));

    SashForm form = new SashForm(shell, SWT.VERTICAL);
    form.setLayout(new GridLayout(1, false));
    form.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    Composite top = new Composite(form, SWT.NONE);
    GridLayout layout = new GridLayout(2, false);
    layout.marginHeight = layout.marginWidth = 0;
    top.setLayout(layout);
    top.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    TableViewer first = createTableViewer(top);
    first.getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    TableViewer second = createTableViewer(top);
    second.getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    TableViewer third = createTableViewer(form);
    third.getTable().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    shell.pack();
    shell.setSize(shell.getSize().x, 400);
    shell.open();

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

private static TableViewer createTableViewer(Composite parent)
{
    TableViewer viewer = new TableViewer(parent, SWT.MULTI | SWT.FULL_SELECTION | SWT.BORDER);

    createColumns(viewer);

    Table table = viewer.getTable();
    table.setHeaderVisible(true);
    table.setLinesVisible(true);

    List<String> data = new ArrayList<>();

    for (int i = 0; i < 20; i++)
        data.add("Item " + i);

    viewer.setContentProvider(ArrayContentProvider.getInstance());
    viewer.setInput(data);

    return viewer;
}

private static void createColumns(TableViewer viewer)
{
    TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
    column.getColumn().setWidth(200);
    column.getColumn().setText("Column");
    column.setLabelProvider(new ColumnLabelProvider()
    {
        @Override
        public void update(ViewerCell cell)
        {
            Object element = cell.getElement();
            if (element instanceof String)
            {
                String obj = (String) element;
                cell.setText(obj);
            }
        }
    });
}

Looks like this:

enter image description here

And after resizing:

enter image description here

Upvotes: 6

Related Questions