2013-01-24 29 views
0

我在打開結果對話框的基礎對話框中有一個按鈕。SWT - 只允許打開1個對話框

private void showPlotResultsDialog() { 
    resultsDialog = new AplotPlotResultsDialog(getShell()); 
    resultsDialog.setBlockOnOpen(true); 
    resultsDialog.open(); 

}

用戶被允許離開結果對話框打開因爲他們的工作。但我最近注意到用戶可以多次點擊「打開結果對話框」。
隨着每次點擊一個新的結果對話框打開。可以打開一些相同的對話框,並使用表中的不同數據。

  1. 是否可以檢查並查看對話框是否在點擊按鈕時已打開?如果一個已經打開,彈出一條消息,說它已經打開,並阻止打開一個新的。

回答

1

當點擊按鈕時是否可以檢查並查看對話框是否已經打開?

當然。只需在您的方法中檢查null。如果該實例不爲null,則會打開一個對話框。

如果一個已經打開,彈出一個消息,說是已經打開,並且塊打開一個新的

它會更好,更新的對話框,將焦點設置在對話框上。保存用戶不得不關閉彈出消息,關閉對話框並打開相同的對話框。

+2

並且不要忘記將字段設置爲'null'後'關閉了。 –

1

另一種可能是給你的shell(應該僅打開一次)的唯一ID與方法:

shell.setData("yourID");

如果你有SelectionListener(例如),您可以檢查Shell ID yourID已打開。

操作:

  • 如果Shell是開放的地方:激活shell(設置焦點)
  • 如果Shell未打開:打開外殼

的例子(見註釋) :

yourButton.addSelectionListener(new SelectionAdapter() { 
    @Override 
    public void widgetSelected(SelectionEvent e) { 

     // Loop through all active shells and check if 
     // the shell is already open 
     Shell[] shells = Display.getCurrent().getShells(); 

     for(Shell shell : shells) { 
      String data = (String) shell.getData(); 

      // only activate the shell and return 
      if(data != null && data.equals("yourID")) { 
       shell.setFocus(); 
       return; 
      } 
     } 

     // open the shell and the dialog 
     Shell shell = new Shell(Display.getCurrent()); 
     shell.setData("yourID"); 
     YourDialog yourDialog = new YourDialog(shell); 
     yourDialog.open(); 
    } 
}); 
+0

我確實讓你的代碼正常工作,但我最終使用了上面的建議。感謝您的回答,因爲我學會了如何爲外殼分配標識並檢查外殼 – jkteater