SWT: Composite as filler

Back to the days when I use Swing, I often use JPanel as a filler to get the layout that I want. Especially if I’m not in the mood of using GridBagLayout or the circumstances just don’t allow me to use that powerful layout.

Now after I work for quite some time with SWT, I faced the same problem again. This time, I decide to use Composite as filler. Unfortunately, the solution is a bit harder than what I expected.

So this is my initial code.

package de.bwb.ims.dialog;

import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;

public class Test extends Composite {

    private Button radioButton = null;
    private Button radioButton1 = null;
    private Composite composite = null;
    private Combo combo = null;
    private ComboViewer comboViewer = null;

    public Test(Composite parent, int style) {
        super(parent, style);
        initialize();
    }

    private void initialize() {
        GridLayout gridLayout = new GridLayout();
        gridLayout.numColumns = 2;
        gridLayout.makeColumnsEqualWidth = true;
        radioButton = new Button(this, SWT.RADIO);
        radioButton.setText("dsdsdsdsds");
        radioButton1 = new Button(this, SWT.RADIO);
        radioButton1.setText("sasasa");
        this.setLayout(gridLayout);
        createComposite();
        createCombo();
        setSize(new Point(300, 200));
    }

    private void createComposite() {
        composite = new Composite(this, SWT.NONE);
    }

    private void createCombo() {
        combo = new Combo(this, SWT.NONE);
        comboViewer = new ComboViewer(combo);
    }

}

Seems OK, right? But the result is not OK.

composite_without_layout

See that space between ’sasasa’ radio button and the combo box? Where is this come from? I try to set the size of composite without luck and it’s a little bit frustrating at once.

After doing the same thing again, I realized that in my previous example I don’t set the layout of the composite. So by adding this line:

composite.setLayout(new GridLayout());

Bla… now I get the correct layout:

composite_layout

It’s frustrating, isn’t it?

Related posts:

  1. Two great tools to help your SWT application development
  2. Be extremely careful when you initialize a non-final field in the declaration!
  3. Eclipse RCP Tips: Drawing dash line
  4. Performance benchmark for logging
  5. Eclipse plugin: Introduce Static Imports

0 Responses to “SWT: Composite as filler”


  • No Comments

Leave a Reply