0

I have a view, where elements are displayed, depending on previous choices. dispose() and parent.layout() do get rid of elements as expected. Question is, how do I get them back? I suppose, dispose() doesn't delete an element, just takes it out of the elements list to render. So, how can I get one back?

Thanx in advance, Marcus


Edit:

Just found, that dispose() frees the object and recursively it's children, so I have to recreate them, once I have them disposed? If so, how can I find the position inside a e.g. GridLayout?


Edit(2):

According to the answer below, I have tried this:

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();
            }
        });
    }   
}

The textfield gest visible as expected, but the lower button doesn't change place? Thanx again for any thoughts...

Baz
  • 36,440
  • 11
  • 68
  • 94
Marcus Toepper
  • 2,403
  • 4
  • 27
  • 42
  • Your code works for me. BTW: The first argument of the constructor of `GridLayout` is used to set the number of columns. You should not use `GridData.Fill_BOTH`, but `1`... – Baz Aug 31 '12 at 07:39
  • @marcus toepper: see my updated answer. – Favonius Aug 31 '12 at 08:26
  • @marcus toepper: Also instead of using `myParent.layout()`, use `textInTheMiddle.getParent().layout();` as your using `this` for `textInTheMiddle = new Text(this, SWT.BORDER);` – Favonius Aug 31 '12 at 08:30
  • done and thanx to both of you 8) – Marcus Toepper Aug 31 '12 at 09:33

1 Answers1

3

Yes, dispose() frees the object and recursively it's children. So you have to recreate the controls/widgets whom you have disposed. In scenario like yours its advisable to hide the controls and re-layout your composite/container. For an example see here

Code !!

The important point is the usage of GridData::exclude property.

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();
    }
}
Community
  • 1
  • 1
Favonius
  • 13,959
  • 3
  • 55
  • 95