Swing Applet losing dirty data
In reaction to a button being pressed on a toolbar, the user is prompted if he wants to Discard his changes (dirty data). If he selects YES that he wants to Discard his changes the displayed applet is stopped and destroyed. Alternatively, if the user selects NO that he does not want to Discard his changes, I trick the application into saving his changes (dirty data). I force a ToolbarController.SAVE event which ties into a SaveAction Thread to force his changes.
I want to allow enough time for the SaveAction thread to do its job, so I wrapped the code in a SwingUtilities.invokeAndWait thread method call.
At runtime, the dirty data is lost. Is the SwingUtilities.invokeAndWait the proper method to use in this case?
Here is the code snippet:
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();
}
}
}
}
Suggestions?
Problem maybe that SwingWorker is not blocking the shutdown procedure. Imagining that theres nothing to refresh in the GUI that makes use of SwingWorker mandatory in this use case i would just run the code that SwingWorker currenlt run but without using the SwingWorker. This way, thinks that are being done in the SwingWorker will block the shutdown process.
精彩评论