2012-12-02 52 views
2

這是我的對話框類:InputDialog是用另一個View的按鈕打開的。該對話框包含單個文本輸入。JFace對話框處理提交的數據(OKPressed)

public class InputDialog extends Dialog{ 
    public InputDialog(Shell parentShell) { 
     super(parentShell); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    protected Control createDialogArea(Composite parent) { 
     parent.setLayout(new GridLayout(1, false)); 

     Text txtName = new Text(parent, SWT.NONE); 

     return super.createDialogArea(parent); 
    } 

    @Override 
    protected void okPressed() { 
     // TODO Auto-generated method stub 
     super.okPressed(); 
    } 
} 

而且我這是怎麼打開的對話框:

buttAdd.addSelectionListener(new SelectionListener() { 

    @Override 
    public void widgetSelected(SelectionEvent e) { 
     // TODO Auto-generated method stub 

     InputDialog dialog = new InputDialog(new Shell()); 
     dialog.open(); 
    } 

    @Override 
    public void widgetDefaultSelected(SelectionEvent e) { 
     // TODO Auto-generated method stub 

    } 
}); 

我如何處理/讀取返回或提交的值的從對話?

回答

6

您可以將輸入的值保存在對話框中的字段中,然後在對話框關閉後使用getter。由於InputDialog被阻塞,你將不得不檢查它的返回值。

if (Window.OK == dialog.open()) { 
    dialog.getEnteredText(); 
} 

其中

public class InputDialog extends Dialog { 
    private Text txtName; 
    private String value; 

    public InputDialog(Shell parentShell) { 
     super(parentShell); 
     value = ""; 
    } 

    @Override 
    protected Control createDialogArea(Composite parent) { 
     parent.setLayout(new GridLayout(1, false)); 

     txtName = new Text(parent, SWT.NONE); 

     return super.createDialogArea(parent); 
    } 

    @Override 
    protected void okPressed() { 
     value = txtName.getText(); 
    } 

    public String getEnteredText() { 
     return value; 
    } 
}