1

I am adding a SWT Label dynamically as follows:

myLabel = new Label(composite, SWT.NONE);
myLabel.setText("CUSTOM MESSAGE");
composite.layout(true, true);
composite.setSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT, true));

Now I am trying to dispose this same widget dynamically as follows:

if(myLabel!=null && !myLabel.isDisposed())
    myLabel.dispose();
composite.layout(true, true);
composite.setSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT, true));
composite.pack(true);

I also tried with and without the last line composite.pack(true).
The page shows the newly added label properly but when I dispose the label, label is disposed but the space occupied by it remains as it is.
I am using this code in my custom eclipse view. Here are the few links I referred:
SWT/JFace: remove widgets
SWT composite - redraw problem
Adding more code:

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.part.ViewPart;

public class MyView extends ViewPart {

    private Composite composite ;
    private Label customLabel ;

    @Override
    public void createPartControl(Composite parent) {

        composite = new Composite(parent, SWT.NONE);
        composite.setLayout(new GridLayout(1, false));

        createTempField("Hello");
        //do stuff
        clearTempField();
        //do stuff
        createTempField("Bye");

    }

    private void createTempField(final String message){
        if(customLabel!=null && !customLabel.isDisposed())
            customLabel.setText(message);
        else if(composite!=null){
            customLabel = new Label(composite, SWT.NONE);
            customLabel.setText(message);
            composite.layout(true, true);
            composite.setSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT, true));
        }
    }

    private void clearTempField() {
        if(customLabel!=null && !customLabel.isDisposed())
            customLabel.dispose();
        composite.layout(true);
        composite.layout(true, true);
        composite.pack(true);
    }

}
Community
  • 1
  • 1
Destructor
  • 3,154
  • 7
  • 32
  • 51

0 Answers0