Java 如何在dispose上删除Eclipse小部件中的侦听器?

Java 如何在dispose上删除Eclipse小部件中的侦听器?,java,eclipse,swt,Java,Eclipse,Swt,我正在SWT中制作一个组件,用于侦听其他组件中的事件。但是,当该组件被释放时,它仍然注册为侦听器。我想知道如何在处理此组件时自动删除此侦听器 我的意思是: public myDialog implements SelectionListener, ModifyListener { public Button myButton = new Button(); public myDialog(){//constructor anotherPage.so

我正在SWT中制作一个组件,用于侦听其他组件中的事件。但是,当该组件被释放时,它仍然注册为侦听器。我想知道如何在处理此组件时自动删除此侦听器

我的意思是:

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
会同时侦听自己的按钮和另一个页面中的按钮。如果用户决定释放
myDialog
(关闭对话框),但它仍会侦听
someButton
,这是没有意义的。如何将其卸下

您有一个
dispose()
API用于
对话框(或
向导
,随便什么)。重写它,并删除其中的侦听器

比如说

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

    super.dispose();
}

可能是
另一页.someButton.removeSelectionListener(此)