Safely open and close modal JDialog (using SwingWorker)
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