简体   繁体   中英

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

I have a table in a Part area need to be displayed when I open my RCP application, however for now I don't know how to make the table auto resize according to Part outside. You can see from my screenshow below which is ugly.

在此处输入图片说明

And following is my source code, I'd really appreciate that if anyone who can show me how to make the table auto resize.

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

Just set the right and bottom attachments for the table to be 100 percent of the width/height with a small negative offset for any border you want:

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

Note: It is not a good idea to change the layout of the parent Composite passed to the @PostConstruct method. Create your own Composite inside the parent composite.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM