当我尝试从“确定”按钮的选择监听器访问 SWT 对话框内的文本字段中的字段值时,收到以下异常。
org.eclipse.swt.SWTException: Widget is disposed
我可以理解该错误是因为当我尝试访问它时对话框已经被释放。但我还没有进行任何显式调用来处理 shell。
单击“确定”按钮后该对话框是否会自动处理?有什么办法可以覆盖它吗?或者我在这里做错了什么?
感谢任何帮助或指示。相关代码片段如下:
public class MyDialog extends Dialog {
/** The file Name Text field. */
private Text fileNameText;
/** The constructor. **/
protected MyDialog(Shell parentShell) {
super(parentShell);
}
/** Create Dialog View. **/
protected Control createDialogArea(Composite parent) {
Composite mainComposite = (Composite) super.createDialogArea(parent);
fileNameText = new Text(mainComposite, SWT.BORDER);
fileNameText.setText("");
fileNameText.setBounds(0, 20, 428, 20);
return mainComposite;
}
/** Override method to set name and listener for 'OK' button. **/
protected void createButtonsForButtonBar(Composite parent) {
super.createButtonsForButtonBar(parent);
Button submitButton = getButton(IDialogConstants.OK_ID);
submitButton.setText("Review");
setButtonLayoutData(submitButton);
// Perform Selected CleanUp activity on Submit Click.
submitButton.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
// do something.
if (fileNameText.getText().isEmpty()) {
return;
}
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
}
});
}
}
最佳答案
是的,单击“确定”按钮时会自动处理该对话框,否,您不应停止此操作。
您必须做的是覆盖okPressed
并在对话框处理之前保存文本值:
private String fileNameValue;
....
@Override
protected void okPressed()
{
fileNameValue = fileNameText.getText();
super.okPressed();
}
关于java - SWT/JFace 对话框中“确定”按钮选择监听器中出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26292021/