如何使用SWT根据RCP应用程序中的外部Part区域自动调整表格大小?

时间:2016-11-28 08:35:36

标签: java swt eclipse-rcp

我在打开RCP应用程序时需要显示 Part 区域中的表格,但是现在我不知道如何根据部分使表格自动调整大小外面。您可以在下面的屏幕显示中看到哪些内容很难看。

enter image description here

以下是我的源代码,如果有人能告诉我如何让表格自动调整大小,我真的很感激。

import java.util.List;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.eclipse.e4.ui.di.Focus;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

import com.hpi.hpdm.console.utils.Device;
import com.hpi.hpdm.console.utils.DeviceRetriever;

public class DeviceListPart {
    private static Table table;

    @PostConstruct
    public void createControls(Composite parent) {

        parent.setLayout(new FormLayout());
        table = new Table(parent, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);

        table.setLinesVisible(true);
        table.setHeaderVisible(true);

        FormData fd_table = new FormData();
        fd_table.bottom = new FormAttachment(0, 465);
        fd_table.right = new FormAttachment(0, 620);
        fd_table.top = new FormAttachment(0, 44);
        fd_table.left = new FormAttachment(0, 10);
        table.setLayoutData(fd_table);


        Button btnRetrieve = new Button(parent, SWT.NONE);

        FormData fd_btnRetrieve = new FormData();
        fd_btnRetrieve.bottom = new FormAttachment(table, -4);
        fd_btnRetrieve.left = new FormAttachment(table, 0, SWT.LEFT);
        btnRetrieve.setLayoutData(fd_btnRetrieve);
        btnRetrieve.setText("Retrieve");

        Button btnAdd = new Button(parent, SWT.NONE);

        FormData fd_btnAdd = new FormData();
        fd_btnAdd.top = new FormAttachment(btnRetrieve, 0, SWT.TOP);
        fd_btnAdd.left = new FormAttachment(btnRetrieve, 6);
        btnAdd.setLayoutData(fd_btnAdd);
        btnAdd.setText("Add");

        Button btnDelete = new Button(parent, SWT.NONE);
        FormData fd_btnDelete = new FormData();
        fd_btnDelete.top = new FormAttachment(btnRetrieve, 0, SWT.TOP);
        fd_btnDelete.left = new FormAttachment(btnAdd, 6);
        btnDelete.setLayoutData(fd_btnDelete);
        btnDelete.setText("Delete");

        String[] titles = { "Device Name", "Description"};
        for (int i = 0; i < titles.length; i++) {
            TableColumn column = new TableColumn(table, SWT.NONE);
            column.setText(titles[i]);
            table.getColumn(i).pack();
        }


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



    }

    @Focus
    public void onFocus() {
        table.setFocus();
    }
}

1 个答案:

答案 0 :(得分:0)

只需将表格的右侧和底部附件设置为宽度/高度的100%,并为所需的任何边框设置小的负偏移量:

fd_table.bottom = new FormAttachment(100, -10);
fd_table.right = new FormAttachment(100, -10);

注意:更改传递给Composite方法的父@PostConstruct的布局不是一个好主意。在父组合内创建自己的Composite