【问题标题】:Error in OK button selection listener in a SWT/JFace DialogSWT/JFace 对话框中的 OK 按钮选择侦听器出错
【发布时间】:2014-10-10 15:25:53
【问题描述】:

我在尝试从“确定”按钮的选择侦听器访问 SWT 对话框中的文本字段中的字段值时收到以下异常。

org.eclipse.swt.SWTException: Widget is disposed

我可以理解该错误是因为当我尝试访问 Dialog 时它已被释放。但我还没有明确调用处置外壳。

对话框是否在单击“确定”按钮时自动释放?有什么办法可以覆盖它吗?还是我在这里做错了什么?

感谢任何帮助或指示。相关代码sn-p如下:

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) {
            }
        });
    }
}

【问题讨论】:

    标签: java eclipse eclipse-plugin swt jface


    【解决方案1】:

    是的,当单击“确定”按钮时,对话框会自动释放,不,你不应该停止它。

    你必须做的是覆盖okPressed并在对话框被释放之前保存文本值:

    private String fileNameValue;
    
    ....
    
    @Override
    protected void okPressed()
    {
      fileNameValue = fileNameText.getText();
    
      super.okPressed();
    }
    

    【讨论】:

    • 谢谢.. 有效。唯一的问题是,视图有许多输入,所有这些都需要在调用okPressed 方法之前分配给变量。
    • 是的,它会变得一团糟。您可以使用JFace data binding 自动设置类中的字段。
    • 谢谢..我也会看看。
    猜你喜欢
    • 2011-07-16
    • 2013-03-23
    • 2018-09-14
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-26
    相关资源
    最近更新 更多