如何隐藏SWT复合材料以使其不占用空间? [英] How to hide an SWT composite so that it takes no space?

查看:201
本文介绍了如何隐藏SWT复合材料以使其不占用空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要隐藏复合材料(以及所有儿童)。只需设置 setVisible(false)即可保留复合材料的空间。

I need to hide a composite (and all children inside). Just setting setVisible(false) will keep the space of the composite.

Composite outer = new Composite(parent, SWT.NONE);      
outer.setLayout(new GridLayout(1,false));
outer.setLayoutData(new GridData(GridData.FILL_BOTH) );

Composite compToHide = new MyComposite(outer, SWT.NONE);        
compToHide.setLayout(new GridLayout());
compToHide.setVisible(false);


推荐答案

这里有一些代码可以满足您的需求。我基本上使用 GridData#exclude Control #setVisible(boolean)一起隐藏/取消隐藏复合

Here is some code that does what you want. I basically use GridData#exclude in combination with Control#setVisible(boolean) to hide/unhide the Composite:

public static void main(String[] args)
{
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("StackOverflow");
    shell.setLayout(new GridLayout(1, true));

    Button hideButton = new Button(shell, SWT.PUSH);
    hideButton.setText("Toggle");

    final Composite content = new Composite(shell, SWT.NONE);
    content.setLayout(new GridLayout(3, false));

    final GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
    content.setLayoutData(data);

    for(int i = 0; i < 10; i++)
    {
        new Label(content, SWT.NONE).setText("Label " + i);
    }

    hideButton.addListener(SWT.Selection, new Listener()
    {
        @Override
        public void handleEvent(Event arg0)
        {
            data.exclude = !data.exclude;
            content.setVisible(!data.exclude);
            content.getParent().pack();
        }
    });

    shell.pack();
    shell.open();
    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

隐藏前:

隐藏后:

这篇关于如何隐藏SWT复合材料以使其不占用空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆