Use GridLayout in Scrolled Composite
Asked Answered
M

2

6

I am trying to created a scrolled composite that has a composite with a grid layout inside.

However when I try to set the content of the scrolled composite nothing loads. This appears only to affect composites with grid layouts.

What am I doing wrong?

My Code:

    CTabItem tbtmNotes = new CTabItem(tabFolder, SWT.NONE);
    tbtmNotes.setText("Notes");
    ScrolledComposite scrollComposite = new ScrolledComposite(tabFolder, SWT.V_SCROLL | SWT.BORDER);
    tbtmNotes.setControl(scrollComposite);
    scrollComposite.setContent(new hm_Composites.Comp_Animal_Notes(tabFolder, SWT.None, a));
Manifold answered 26/4, 2012 at 17:3 Comment(0)
M
10

After a bunch of fiddling I got the following to work hope it helps someone else out.

    public Comp_Animal_Notes(Composite parent, int style, Animal a)
        throws Exception {
    super(parent, SWT.NONE);
    setLayout(new FillLayout(SWT.HORIZONTAL));
    ScrolledComposite scrolledComposite = new ScrolledComposite(this, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    scrolledComposite.setExpandHorizontal(true);
    scrolledComposite.setExpandVertical(true);

    Composite composite = new Composite(scrolledComposite, SWT.NONE);
    composite.setLayout(new GridLayout(2, false));

    ArrayList<Note> alNotes = a.getAnimalNotes();
    this.setRedraw(false);
    for (int i = 0; i < alNotes.size(); i++) {
        Note note = alNotes.get(i);

        CLabel lblNoteDate = new CLabel(composite, SWT.BORDER);
        lblNoteDate.setFont(SWTResourceManager.getFont("Tahoma", 8,
                SWT.BOLD));
        lblNoteDate.setText("Date:");
        lblNoteDate.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));

        CLabel lblNotedatetxt = new CLabel(composite, SWT.BORDER);
        lblNotedatetxt.setBackground(SWTResourceManager
                .getColor(SWT.COLOR_WHITE));
        lblNotedatetxt.setText(note.getUserDate());
        lblNotedatetxt.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

        CLabel lblNoteTxt = new CLabel(composite, SWT.BORDER);
        lblNoteTxt.setBackground(SWTResourceManager
                .getColor(SWT.COLOR_WHITE));
        lblNoteTxt.setText(note.getStrNote());
        lblNoteTxt.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
    }

    scrolledComposite.setContent(composite);
    scrolledComposite.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));

    this.setRedraw(true);
}
Manifold answered 27/4, 2012 at 13:43 Comment(0)
B
5

If snippet from Talon06 does not work, try make following changes.

scrolledComposite.setContent(composite);

//add this line
composite.setSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));

//not sure about this line, was optional in my case
scrolledComposite.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT));

this.setRedraw(true);

I found that components within ScrolledComposite were only getting visible if I explicitly set size of composite.

@Talon06 : Thanks for your awesome finding!

Bul answered 9/10, 2012 at 22:4 Comment(1)
Thanks for the changes this makes the composite visible in the Windows Builder Design tab for me making things go much quicker.Manifold

© 2022 - 2024 — McMap. All rights reserved.