2016-11-28 32 views
0

我有一个部分表需要显示,当我打开我的RCP应用程序,但现在我不知道如何使表自动调整大小根据部分外面。你可以从我的屏幕下方看到哪个是丑陋的。如何使用SWT根据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(); 
    } 
} 

回答

0

刚刚成立的右侧和底部附件,该表是宽度/高度的100%以小负给你想要的任何边界偏移:

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

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

+0

感谢您的建议。有用。您能否请我指出正确的方式来创建我自己的'复合材料'而不会破坏我想要的GUI效果?我试过'Composite ownComp = new Composite(parent,SWT.NONE); ownComp.setLayout(new FormLayout());',但它不是我想要的。 – kenshinji

+0

为什么不是你想要的?不要忘记将所有剩余的对'parent'的引用改为'ownComp'。 –

相关问题