Search code examples
javaswingwindowlistener

Java swing closing message box


When the user closes my program, the program needs to shut down some background processes that might take a while to shutdown. So I want the user to be notified with a message box appearing and informing the user, and then when the background processes are all done, the message box should disappear.

An example of something similar to this is when you close Eclipse a message box will appear, notifying the user that the environment is being saved along with other shutdown processes.

How could I go about doing this?


Solution

  • You can add a WindowListener to your top level JFrame, listen for the windowClosing event, and then pop up a dialog box. In terms of making the dialog dismiss itself, you'll probably want to use a JProgressBar to inform user how much time is left (see this tutorial). Assuming you're using a SwingWorker, you can have the SwingWorker doing the work call the dispose method of the JDialog when it's finished processing (within its done method).