【发布时间】:2011-09-25 03:35:00
【问题描述】:
在工具栏上按下按钮时,会提示用户是否要放弃他的更改(脏数据)。如果他选择“是”表示他想放弃他的更改,则显示的小程序将停止并销毁。或者,如果用户选择“否”表示他不想放弃他的更改,我会欺骗应用程序保存他的更改(脏数据)。我强制 ToolbarController.SAVE 事件绑定到 SaveAction 线程以强制他的更改。
我想留出足够的时间让 SaveAction 线程完成其工作,因此我将代码包装在 SwingUtilities.invokeAndWait 线程方法调用中。
在运行时,脏数据会丢失。 SwingUtilities.invokeAndWait 是在这种情况下使用的正确方法吗?
这里是sn-p的代码:
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();
}
}
}
}
建议?
【问题讨论】:
-
如需尽快获得更好的帮助,请发帖SSCCE。
标签: java multithreading swing user-interface applet