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

How to make table auto resize according to the outside Part area in RCP application by using SWT?

我在 部分有一个 table 区域需要在我打开我的 RCP 应用程序时显示,但是现在我不知道如何制作table 自动根据 Part 外部调整大小。你可以从我下面的屏幕截图中看到它是丑陋的。

下面是我的源代码,如果有人能告诉我如何使 table 自动调整大小,我将不胜感激。

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

只需将 table 的右侧和底部附件设置为 width/height 的 100%,并为您想要的任何边框设置一个小的负偏移:

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

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