SWT JFace如何在moveBelow()之后重绘

时间:2012-08-31 05:27:04

标签: java swt dispose jface

我有一个视图,根据之前的选择显示元素。 dispose()和parent.layout()可以按预期去掉元素。问题是,我该如何取回它们?我想,dispose()不会删除元素,只是从元素列表中取出它来呈现。那么,我怎么才能回来?

Thanx提前, 马库斯

<小时/> 修改

刚刚发现,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();
            }
        });
    }   
}

textfield gest按预期显示,但下方按钮不会改变位置? 再次对任何想法感谢...

1 个答案:

答案 0 :(得分:3)

是的, dispose()释放对象并递归地释放它的孩子。因此,您必须重新创建已处置的控件/窗口小部件。在像您这样的场景中,建议隐藏控件并重新布局复合/容器。例如see here

代码!!

重点是使用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();
    }
}