I needed a way to get some data from a database and prevent the user from modifying existing data for that moment.
I created a SwingWorker to make the db update and a modal JDialog to show user what is going on (with a JProgressBar). The modal dialog has defaultCloseOperation set to DO_NOTHING, so it can only be closed with a proper call - I use setVisible(false)
.
MySwingWorkerTask myTask = new MySwingWorkerTask();
myTask.execute();
myModalDialog.setVisible(true);
The SwingWorker does some stuff within doInBackground() and lastly it calls:
myModalDialog.setVisible(false);
My only concern and my question:
Is is possible that the SwingWorker executes the setVisible(false)
before it is setVisible(true)
in the line after worker spawn?
If so the setVisible(true)
could block forever (the user can't close the modal window).
Do I have to implement something as:
while (!myModalDialog.isVisible()) {
Thread.sleep(150);
}
myModalDialog.setVisible(false);
to make sure it will actually get closed?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…