Search code examples
javatabsswtscrolledcomposite

ScrolledCompsite in TabFolder sizes content to zero


I have the following problem:
I'm using SWT to create a GUI for my application. I have a TabFolder in which I add several TabItems and in each of those I create a ScrolledComposite that holds some content.

The TabFolder is displaying fine, however the ScrolledComposite in the TabFolder does only show it's content in the first TabItem. All other ScrolledComposites are visible themselves just fine but their content is invisible.

Here is a little code snippet that demonstrates what I am referring to:

    Display display = new Display();

    Shell topShell = new Shell(display);

    topShell.setSize(800, 800);
    topShell.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
    topShell.setLayout(new FillLayout());


    TabFolder folder = new TabFolder(topShell, SWT.NONE);

    for (int i = 0; i < 5; i++) {
        TabItem item = new TabItem(folder, SWT.NONE);
        item.setText("Item " + i);

        ScrolledComposite scroller = new ScrolledComposite(folder,
                SWT.H_SCROLL | SWT.V_SCROLL );

        scroller.setBackground(display.getSystemColor(SWT.COLOR_BLUE));

        Composite content = new Composite(scroller, SWT.NONE);
        content.setBackground(display.getSystemColor(SWT.COLOR_RED));

        scroller.setContent(content);
        scroller.setExpandHorizontal(true);
        scroller.setExpandVertical(true);

        item.setControl(scroller);
    }

    topShell.setVisible(true);

    while (!topShell.isDisposed()) {
        display.readAndDispatch();
    }

You can tell that the content is being displayed if the area is painted red. If the content is invisible the area is blue (the background of the ScrolledComposite)

I'm not sure if that matters but this occurs on Linux Mint 18 and it appears to only happen within GTK 3 (in 2 it works just fine)

After quite some time I tracked the issue down to the following:
It turned out that the problem was that the "missing" content had a size of zero, because the layouting doesn't set the size of those.

In my case it could be fixed by removing the SWT.V_SCROLL and the SWT.H_SCROLL style constants from the ScrolledComposite. Therefore the above code written as following works as expected.

    Display display = new Display();

    Shell topShell = new Shell(display);

    topShell.setSize(800, 800);
    topShell.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
    topShell.setLayout(new FillLayout());


    TabFolder folder = new TabFolder(topShell, SWT.NONE);

    for (int i = 0; i < 5; i++) {
        TabItem item = new TabItem(folder, SWT.NONE);
        item.setText("Item " + i);

        ScrolledComposite scroller = new ScrolledComposite(folder,
                SWT.NONE);

        scroller.setBackground(display.getSystemColor(SWT.COLOR_BLUE));

        Composite content = new Composite(scroller, SWT.NONE);
        content.setBackground(display.getSystemColor(SWT.COLOR_RED));

        scroller.setContent(content);
        scroller.setExpandHorizontal(true);
        scroller.setExpandVertical(true);

        item.setControl(scroller);
    }

    topShell.setVisible(true);

    while (!topShell.isDisposed()) {
        display.readAndDispatch();
    }

Although that causes all content to be properly sized it completely removes the ScrollBars of the ScrolledComposite which somehow is not what you want from a ScrolledComposite.

Does anyone know how to fix that or whether that is a bug (that might have been fixed in newer SWT versions)?


Solution

  • I've fixed bugs related to this a few months ago.

    Can you try latest SWT master? http://download.eclipse.org/eclipse/downloads/