1

I have a class extending from ScrolledComposite, within this class I have created a Composite which is intended to fill the parent ScrolledComposite. But whenever I add labels to the inner composite it wraps to the upper right corner only. I want my inner Composite to completely fill the outer ScrolledComposite. Is there a way to do this?

    public class Container extends ScrolledComposite {

    final Composite comp;
    Label nameLabel, sizeLabel, dateLabel;
    ArrayList<Label> entrySize, date;
    ArrayList<SimpleEntry> entry;
    Color white;
    Font font;

    public Container(Composite parent, int style) {
        super(parent, style);
        white = new Color(getDisplay(), 255, 255, 255);
        font = new Font(getDisplay(), "Arial", 16, SWT.BOLD);
        setBackground(white);
        setLayout(new GridLayout(1, false));

        comp = new Composite(this, SWT.BORDER);
        GridData compData = new GridData(SWT.FILL, SWT.FILL, true, true);
        comp.setLayoutData(compData);
        setContent(comp);
        comp.setBackground(white);
        comp.setLayout(new GridLayout(3, false));

        nameLabel = new Label(comp, SWT.BOLD);
        sizeLabel = new Label(comp, SWT.BOLD);
        dateLabel = new Label(comp, SWT.BOLD);

        GridData d1 = new GridData(SWT.CENTER, SWT.CENTER, true, false);
        nameLabel.setLayoutData(d1);

        GridData d2 = new GridData(SWT.CENTER, SWT.CENTER, true, false);
        sizeLabel.setLayoutData(d2);

        GridData d3 = new GridData(SWT.CENTER, SWT.CENTER, true, false);
        dateLabel.setLayoutData(d3);

        nameLabel.setBackground(white);
        sizeLabel.setBackground(white);
        dateLabel.setBackground(white);

        nameLabel.setText("Name");
        sizeLabel.setText("Size");
        dateLabel.setText("Last Modified");

        nameLabel.setFont(font);
        sizeLabel.setFont(font);
        dateLabel.setFont(font);

        comp.setSize(comp.computeSize(SWT.DEFAULT, SWT.DEFAULT));
        comp.layout();

        addDisposeListener(new DisposeListener() {
            @Override
            public void widgetDisposed(DisposeEvent e) {
                Container.this.widgetDisposed(e);
            }
        });
    }
}
Baz
  • 36,440
  • 11
  • 68
  • 94
Anas
  • 177
  • 1
  • 11

1 Answers1

3

Just add these lines at the end of the constructor:

this.setMinSize(comp.computeSize(SWT.DEFAULT, SWT.DEFAULT));
this.setExpandHorizontal(true);
this.setExpandVertical(true);

Notes:

  • Remember to dispose() all resources you create yourself (Color, Font, ...)
  • Consider using system resources (no need to dispose) by using display.getSystemColor(SWT.COLOR_WHITE) and display.getSystemFont().
Baz
  • 36,440
  • 11
  • 68
  • 94
  • thank you @Baz. Yes I have disposed them off in a separate function not shown here. – Anas Jul 24 '13 at 08:40
  • I thought it would gonna do it since it filled the parent, but after populating the data I have come to know the parent does not scroll now :/ – Anas Jul 24 '13 at 12:04
  • @Anas What happens if you set the min size again after adding your content? – Baz Jul 24 '13 at 12:06
  • yup it is working fine now... Thanks alot.. One more thing, Do I have to add a scroll listener to make it scroll through my mouse scroll? It begins to scroll only when I click an item from the data.. – Anas Jul 24 '13 at 12:10
  • @Anas You shouldn't have to do that. It's working fine for me without any `Listener`. – Baz Jul 24 '13 at 12:12
  • hmm.. Its working for me as well but only after I click an item from the list. Moreover, it is scrolling too slowly. – Anas Jul 24 '13 at 12:14
  • @Anas Consider making this a new question, since it's relatively unrelated to your original question. – Baz Jul 24 '13 at 12:33