简体   繁体   中英

Resizing a composite within a composite

I'm trying to build an eclipse plugin view where I have a view that has an input bar at the top and a TableViewer at the bottom. Currently I'm trying to build 2 separate composites within a larger composite, but I'm having a hard time controlling the size of each of the individual composites. Here is some code:

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

I'm trying to shrink composite 2 while increasing the size of composite1, both of which reside in the parent composite. I've tried looking through methods for the FillLayout as well as the composites themselves but I haven't found anything that works.

Thanks for any help

Try this one:)

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

}

Citing the first sentence of the JavaDoc for FillLayout:

FillLayout is the simplest layout class. It lays out controls in a single row or column, forcing them to be the same size.

So you cannot control the size of composite and composite2, when you use that layout.

You could try using a GridLayout, and set a heightHint on the GridData for composite2.

But IMO the best option for layouts in SWT is Miglayout - once you get it, it is extremely powerful, doesn't need talkative code, and is actually readable. And since you can use it for Swing as well, you have to learn it only once. Try it.

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