在复合材料中调整复合材料的大小

时间:2012-07-13 21:18:17

标签: java eclipse eclipse-plugin swt

我正在尝试构建一个eclipse插件视图,其中我的视图顶部有一个输入栏,底部有一个TableViewer。目前我正在尝试在更大的复合材料中构建2个单独的复合材料,但我很难控制每个复合材料的尺寸。这是一些代码:

public void createPartControl(Composite parent) {

        FillLayout parentLayout = new FillLayout(SWT.VERTICAL);
        parent.setLayout(parentLayout);

        Composite composite = new Composite(parent, SWT.BORDER);
        composite.setLayout(new FillLayout());
        viewer = new TableViewer(composite, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);

        Composite composite2 = new Composite(parent, SWT.BORDER);


        GridLayout gridLayout = new GridLayout();
        composite2.setLayout(gridLayout); 

        GridData expGridData = new GridData();
        expGridData.horizontalAlignment = GridData.FILL;
        expGridData.grabExcessHorizontalSpace = true;
        expGridData.grabExcessVerticalSpace = true;
        Label expLabel = new Label(composite2, SWT.NONE);
        expLabel.setText("Express");
        Text exp = new Text(composite2, SWT.BORDER);
        exp.setLayoutData(expGridData); 
}

我正在尝试缩小composite 2,同时增加composite1的站点,这两个站点都位于父组合中。我已经尝试了FillLayout的方法以及复合材料本身,但我没有发现任何有效的方法。

感谢您的帮助

2 个答案:

答案 0 :(得分:3)

试试这个:)

public void createPartControl(Composite parent) {
    GridData parentData = new GridData(SWT.FILL, SWT.FILL, true, true);
    parent.setLayout(new GridLayout(1, true));
    parent.setLayoutData(parentData);

    Composite searchMenu = new Composite(parent, SWT.BORDER);
    searchMenu.setLayout(new GridLayout(2, false));
    searchMenu.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false));

    Text searchText = new Text(searchMenu, SWT.NONE);
    searchText.setLayoutData(new GridData(SWT.FILL, GridData.CENTER, true, false));
    searchText.setText("search text here");

    Button searchButton = new Button(searchMenu, SWT.PUSH);
    searchButton.setText("Search");

    Composite tableViewerComposite = new Composite(parent, SWT.BORDER);
    tableViewerComposite.setLayout(new GridLayout(1, true));
    tableViewerComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    TableViewer tableViewer = new TableViewer(tableViewerComposite);
    TableViewerColumn column = new TableViewerColumn(tableViewer, SWT.NONE);
    column.getColumn().setWidth(200);
    column.setLabelProvider(new ColumnLabelProvider(){

        @Override
        public String getText(Object element) {
            return element != null ? element.toString() : "null";
        }});

    tableViewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    tableViewer.setContentProvider(ArrayContentProvider.getInstance());
    tableViewer.setInput(new String[]{"one", "two", "three"});

}

答案 1 :(得分:0)

引用JavaDoc for FillLayout的第一句话:

  

FillLayout是最简单的布局类。它在单个行或列中布置控件,强制它们具有相同的大小。

因此,当您使用该布局时,无法控制复合和复合2的大小。

您可以尝试使用GridLayout,并在GridData上为composite2设置heightHint。

但是IMO是SWT中布局的最佳选择是Miglayout - 一旦你得到它,它就非常强大,不需要健谈的代码,而且实际上是可读的。既然你也可以将它用于Swing,你只需要学习一次。试试吧。