Search code examples
javaeclipseeclipse-pluginswt

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


Solution

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