Domanda

I'm making a component in SWT which listens to events in other components. However, when that component is disposed, it is still registered as a listener. I want to know how to get rid of this listener automatically when the this component is being disposed.

Here's what I mean:

public myDialog implements SelectionListener, ModifyListener {
     public Button myButton = new Button();

     public myDialog(){//constructor
             anotherPage.someButton.addSelectionListner(this);
             myButton.addSelectionListner(this);
     }

     public void widgetSelected(SelectionEvent e){
          if(e.getSource()==anotherPage.someButton){
               //do something
          }
          else if(e.getSource()==myButton){
               //do something else
          }
     }
}

    public anotherPage extends AbstractSystemWizardPage{
        public Button someButton=new Button();
        //...
    }

myDialog listens to both its own button and a button in anotherPage. It doesn't make sense if user decides to dispose myDialog (close the dialog) but it still listens to someButton. How do I remove it on dispose?

È stato utile?

Soluzione

You have a dispose() API for the Dialog (or Wizard, whatever). Override it, and remove the listener inside it.

For example

@Override
public void dispose()
{
    anotherPage.someButton.removeSelectionListener(this);

    super.dispose();
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top