2011-09-25 48 views
0

作爲對工具欄上按下按鈕的反應,用戶會被提示是否要放棄其更改(髒數據)。如果他選擇「是」,他想放棄其更改,則顯示的小程序將停止並銷燬。或者,如果用戶選擇不希望放棄其更改的NO,我會誘使應用程序保存其更改(髒數據)。我強制一個ToolbarController.SAVE事件綁定到一個SaveAction線程來強制他的更改。Swing Applet丟失髒數據

我想讓SaveAction線程有足夠的時間來完成它的工作,所以我將代碼包裝在一個SwingUtilities.invokeAndWait線程方法調用中。

在運行時,髒數據丟失。 SwingUtilities.invokeAndWait是在這種情況下使用的正確方法嗎?

這裏是代碼片段:

public void shutdown() { 
    if (configurationManager.isModifedConfigurations()) { 
      int selection = 999; 

     // Discard changes ? 
     selection = JOptionPane.showConfirmDialog(null, messages.getString("ConfigPowerbarChangeConfirmMsg"), 
       messages.getString("ConfigPowerbarChangeConfirmMsgTitle"), 
       JOptionPane.YES_NO_OPTION); 

     if (selection == JOptionPane.NO_OPTION) { //Discard Changes 
      configurationManager.setModifedConfigurations(false); 

      super.shutdown(); 
      removeBindings(); 
      stopCurrentApplet(); 
     } else if (selection == JOptionPane.YES_OPTION) { //Force Save changes 
       System.out.println("Catch ApplicationEvent !!! Force Save of Mutable Table fields."); 

      try { 
          SwingUtilities.invokeAndWait(new Runnable() { 
           public void run() { 
            toolbarController = new ToolbarController(ToolbarModelFactory.getSystemwideToolbarModel()); 
             ApplicationEvent evt = new ApplicationEvent(ToolbarController.SAVE, toolbarController); 
             toolbarController.handleApplicationEvent(evt); 
            } 
          }); 
        } catch (InterruptedException e) { 
          e.printStackTrace(); 
        } catch (InvocationTargetException e) { 
          e.printStackTrace(); 
        } 
      } 
    } 
} 

建議?

+2

如需更快獲得更好的幫助,請發佈[SSCCE](http://pscode.org/sscce.html)。 –

回答

0

問題可能是SwingWorker沒有阻止關閉過程。想象一下,在這個用例中強制使用SwingWorker的GUI中沒有什麼要刷新的,我只需運行SwingWorker運行的代碼,但不使用SwingWorker。這樣,認爲在SwingWorker中正在完成將阻止關機過程。