2014-10-10 46 views
0

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

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

回答

1

是在单击确定按钮时,会自动设置对话框,不,你不应该阻止这一切。

你必须做什么是覆盖okPressed并设置对话框之前保存的文本值:

private String fileNameValue; 

.... 

@Override 
protected void okPressed() 
{ 
    fileNameValue = fileNameText.getText(); 

    super.okPressed(); 
} 
+0

谢谢..奏效。唯一的问题是,视图有很多输入,所有这些都需要在调用'okPressed'方法之前分配给变量。 – 2014-10-10 09:08:17

+0

是的,它可能会变得混乱。您可以使用[JFace数据绑定](http://www.vogella.com/tutorials/EclipseDataBinding/article.html)自动设置类中的字段。 – 2014-10-10 09:10:47

+0

谢谢..我会看看同样的。 – 2014-10-10 12:33:12