使控件可调整大小

时间:2015-01-20 12:50:17

标签: java swt composite

我想在Eclipse中创建一个页面,它有两个彼此相邻的树组件和一个树下面的表。

我编写的代码确实如此,但我也希望表和树可以调整大小。当我调整大小使桌子变小时,树木应该变大。

我尝试了一些我在互联网上找到的不同方法,但没有一种方法可行。

这是我写的代码:

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

2 个答案:

答案 0 :(得分:6)

使用SashForm可以调整组件的大小。这是一个例子:

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

看起来像这样:

enter image description here

调整大小后:

enter image description here

答案 1 :(得分:-1)

见下面的代码:

@PostConstruct
public void postConstruct(Composite parent, MApplication app) {
    parent.setLayout(new GridLayout(1, false));

    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
    composite.setLayout(new GridLayout(2, false));

    TreeViewer treeViewerLeft = new TreeViewer(composite, SWT.BORDER);
    Tree treeLeft = treeViewerLeft.getTree();
    treeLeft.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

    TreeViewer treeViewerRight = new TreeViewer(composite, SWT.BORDER);
    Tree treeRight = treeViewerRight.getTree();
    treeRight.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

    TableViewer tableViewer = new TableViewer(composite, SWT.BORDER | SWT.FULL_SELECTION);
    table = tableViewer.getTable();
    table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
    table.setContentProvider(ArrayContentProvider.getInstance());
    createTableColumns();    
}