java - SWT/JFace 对话框中“确定”按钮选择监听器中出现错误

标签 java eclipse eclipse-plugin swt jface

当我尝试从“确定”按钮的选择监听器访问 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/

相关文章:

java - ArrayIndexOutOfBoundsException : 4096 while reading gif file

java - 即使 jar 位于/lib 中,也会抛出 "java.lang.ClassNotFoundException: org.apache.commons.io.FileUtils"

Eclipse:如何以编程方式刷新当前项目

java - 使用 ArrayList 填充 ListView?

java - 为 Java 开发设置开发环境(Q.1)

Java AudioSystem 防止歌曲结束前循环重复

java - 如何从 Eclipse JDT Parser 解析的 java 源代码中获取行?

eclipse - 如何刷新 FilteredItemsSelectionDialog

eclipse - Eclipse 包和插件有什么区别?

java - 如何创建一个 Eclipse 插件来自动创建现有类的读/写外部方法