0

我有一个视图,其中显示元素,具体取决于先前的选择。dispose() 和 parent.layout() 确实按预期摆脱了元素。问题是,我如何让他们回来?我想, dispose() 不会删除一个元素,只是将它从元素列表中取出来呈现。那么,我怎样才能找回一个呢?

提前谢谢,马库斯


编辑:

刚刚发现, dispose() 释放对象并递归地释放它的孩子,所以我必须重新创建它们,一旦我将它们处理掉?如果是这样,我怎样才能在例如 GridLayout 中找到位置?


编辑(2):

根据下面的答案,我试过这个:

public class TestTab extends Composite 
{
    org.eclipse.swt.layout.GridLayout layout = null;

    GridData griddata = null;

    Button upperButton = null;
    Text textInTheMiddle = null;
    Button lowerButton = null;

    public InvoiceTab(Composite parent, int arg2) {

        super(parent, arg2);
        final Composite myParent = parent;

        layout = new org.eclipse.swt.layout.GridLayout(GridData.FILL_BOTH,
                false);
        layout.numColumns = 1;

        this.setLayout(layout);


        upperButton = new Button(this, SWT.PUSH);
        upperButton.setText("upper Button");

        textInTheMiddle = new Text(this, SWT.BORDER);

        lowerButton = new Button(this, SWT.PUSH);
        lowerButton.setText("lower Button");

        upperButton.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {

                if (textInTheMiddle.isVisible()) {
                    textInTheMiddle.setVisible(false);
                    lowerButton.moveBelow(upperButton);
                } else {
                    textInTheMiddle.setVisible(true);
                    lowerButton.moveBelow(textInTheMiddle);
                }
                myParent.layout();
            }
        });
    }   
}

文本字段按预期显示,但下部按钮没有改变位置?再次感谢任何想法......

4

1 回答 1

3

是的,dispose() 释放对象并递归地释放它的 children。因此,您必须重新创建已处置的控件/小部件。在像您这样的情况下,建议隐藏控件并重新布局您的复合/容器。有关示例,请参见此处

代码 !!

重要的一点是GridData::exclude属性的使用。

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
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;
import org.eclipse.swt.widgets.Text;

class TestTab extends Composite 
{
    Button upperButton = null;
    Text textInTheMiddle = null;
    Button lowerButton = null;

    boolean state = true;

    public TestTab(Composite parent, int style) 
    {
        super(parent, style);


        GridLayout layout = new GridLayout(1, false);
        setLayout(layout);


        upperButton = new Button(this, SWT.PUSH);
        upperButton.setText("upper Button");

        textInTheMiddle = new Text(this, SWT.BORDER);
        GridData data = new GridData(SWT.FILL, SWT.FILL, true, false);
        data.exclude = false;
        textInTheMiddle.setLayoutData(data);

        lowerButton = new Button(this, SWT.PUSH);
        lowerButton.setText("lower Button");

        upperButton.addSelectionListener(new SelectionAdapter() 
        {
            public void widgetSelected(SelectionEvent e) {


                GridData griddata = (GridData) textInTheMiddle.getLayoutData();
                griddata.exclude = state;
                textInTheMiddle.setVisible(!state);
                textInTheMiddle.getParent().layout(false);
                state = !state;
            }
        });
    }   

}
public class Test
{
    public static void main(String[] args) 
    {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new GridLayout(1, false));

        new TestTab(shell, SWT.NONE);

        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}
于 2012-08-31T06:23:19.397 回答