2014-05-17 115 views
0

我有一個對話框,它擴展了TrayDialog以從用戶收集一些數據。我想將收集的值傳遞給另一個類而不關閉對話框。如果我重寫okPressed()方法,數據將被傳回,但窗口將被關閉。保持TrayDialog打開並提交數據

public class MyDialog extends TrayDialog { 
    public MyDialog(final Shell shell, final MyData mydata) { 
     super(shell); 
     this.shell = shell; 
     this.mydata = mydata; 
    } 

    @Override 
    protected void okPressed() { 
     if (!validateData()) { 
      return; 
     } 
     super.okPressed(); 
    } 

    //rest of the code for text box, buttons in the dialog 
} 

final MyDialog myDialog = new MyDialog(new Shell(), this.mydata); 
if (myDialog.open() == Window.CANCEL) { 
    return null; 
} 
//here get the data that user has entered in the dialog after they press ok 

回答

2

如果您不撥打super.okPressed()對話框將不會關閉。 Source

protected void okPressed() { 
    setReturnCode(OK); 
    close(); 
} 

而且爲了將數據傳回okPressed行動,我會建議提供一個回調MyDialog。類似這樣的:

public interface MyCallback { 
    void okPressed(MyData data); 
} 

public class MyDialog extends TrayDialog { 
public MyDialog(final Shell shell, final MyData mydata, MyCallback callback) { 
    super(shell); 
    this.shell = shell; 
    this.mydata = mydata; 
    this.callback = callback; 
} 

@Override 
protected void okPressed() { 
    if (!validateData()) { 
     return; 
    } else { 
     callback.onPressed(data); 
    } 
    //super.okPressed(); 
} 

MyCallback callback = new MyCallback() { 

    void okPressed(MyData data) { 
     // handle data 
    } 
} 
final MyDialog myDialog = new MyDialog(new Shell(), this.mydata, callback); 
    if (myDialog.open() == Window.CANCEL) { 
     return null; 
    } 

在這種情況下,您需要在某些時候手動關閉對話框。此外,如果您使用setBlockOnOpen(false);

,Dialog將不會阻止執行線程