2011-09-25 53 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中正在完成将阻止关机过程。