我正在 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
监听其自己的按钮和 anotherPage
中的按钮。如果用户决定处置myDialog
(关闭对话框),但它仍然监听someButton
,那么这是没有意义的。处置时如何将其删除?
最佳答案
您有一个用于Dialog
(或Wizard
,等等)的dispose()
API。覆盖它,并删除其中的监听器。
例如
@Override
public void dispose()
{
anotherPage.someButton.removeSelectionListener(this);
super.dispose();
}
关于java - 如何在处置时删除 Eclipse Widget 中的监听器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22276616/