如何在ScrolledComposite SWT中进行自动刷新

时间:2012-02-14 14:01:30

标签: java swt

我有一个问题,在我构建我的shell并打开它之后,我需要在运行时打开后向shell添加图形元素 而且一旦shell打开我就看不到添加的新图形元素了。 出现以下新图形元素,以防我调整外壳尺寸。

有没有办法解决此问题并自动查看刷新 这是一个简单的例子: 1-我在选项卡文件夹中添加了10个A按钮,然后打开shell。 2-然后我在壳中添加了10个B按钮 3-如果调整外壳大小,我可以看到按钮B

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;


public class TabFolder
{

    public static void main(String[] args)
    {
        Display display = new Display();

        final Shell shell = new Shell(display);
        shell.setSize(500, 500);
        shell.setLayout(new GridLayout());
        final CTabFolder folder = new CTabFolder(shell, SWT.BORDER);
        folder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        folder.setSize(500, 500);

        CTabItem item = new CTabItem(folder, SWT.CLOSE);

        final ScrolledComposite scrollComp = new ScrolledComposite(folder, SWT.V_SCROLL | SWT.H_SCROLL);
        item.setControl(scrollComp);

        final Composite tab1Comp = new Composite(scrollComp, SWT.NONE);
        tab1Comp.setLayout(new GridLayout(1, true));
        tab1Comp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

        scrollComp.setContent(tab1Comp);
        scrollComp.setExpandVertical(true);
        scrollComp.setExpandHorizontal(true);
        scrollComp.addControlListener(new ControlAdapter()
        {
            public void controlResized(ControlEvent e)
            {
                Rectangle r = scrollComp.getClientArea();
                scrollComp.setMinSize(folder.computeSize(r.width, SWT.DEFAULT));
            }
        });

        for (int x = 0; x < 10; x++)
        {
            Button text = new Button(tab1Comp, SWT.NONE);
            text.setText("A");
        }

        shell.open();

        for (int x = 10; x < 20; x++)
        {
            Button text = new Button(tab1Comp, SWT.NONE);
            text.setText("B");
        }
        while (!shell.isDisposed())
        {
            if (!display.readAndDispatch())
                display.sleep();
        }

        display.dispose();
    }
}

感谢。

1 个答案:

答案 0 :(得分:0)

添加B按钮后,您必须致电tab1Comp.layout(),因为在程序化更改后布局不会自动更新。您还必须更新ScrolledComposite的最小尺寸,因为在编程更改后也不会调用controlResized(...)