By default, when you create a JFrame in a Java client-side application, and the user presses the exit button on the JFrame, the JFrame window is closed, but the application continues to run, essentially running in the background, as you typically don't have a way of showing that JFrame again.
Of course what you really want to do is prompt the user with some type of "You're about to quit the application -- are you sure?" dialog, or just quit the application if you don't want to be that nice. In this article we'll demonstrate how to close the frame, and your application, when your application receives a "close window" (close frame) event. There are several ways to do this, and I'll show each of them in this article.
1) Handling the window closing event with a WindowListener class
There are several ways to accomplish this. The first way is to add a WindowListener
to your JFrame. Step 1 in this process is to create your WindowListener
. I'll show my WindowListener
code here as a separate class, but you can also create this as an anonymous inner class if you prefer:
class MyWindowListener implements WindowListener { public void windowClosing(WindowEvent arg0) { System.exit(0); } public void windowOpened(WindowEvent arg0) {} public void windowClosed(WindowEvent arg0) {} public void windowIconified(WindowEvent arg0) {} public void windowDeiconified(WindowEvent arg0) {} public void windowActivated(WindowEvent arg0) {} public void windowDeactivated(WindowEvent arg0) {} }
As you can see from this code, there is a lot of "do nothing" code in this window listener code (boilerplate methods that provide no functionality), with one method (windowClosing
) that actually does what you want -- it calls System.exit()
when the "window closing" event is fired. Note that all of the "do nothing" methods I refer to are required to fulfill the requirements of the WindowListener
interface.
After creating this class all you need to do is find a reference to your JFrame, and add a reference to an instance of your WindowListener, like this:
myJFrame.addWindowListener(new MyWindowListener());
This can be nice when you want to override several of these event methods, but is overkill if you just want to handle this one event.
2) Handling the window closing event with a WindowAdapter
If you don't want to see the overhead of all those do-nothing methods you can use a WindowAdapter
instead of implementing the WindowListener
interface yourself. The WindowAdapter
implements the WindowListener
interface, so you don't have to. It's intended for making code like this a lot simpler. You just need to create an instance of a WindowAdapter
and override the windowClosing
method, like this:
myJFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { System.exit(0); } });
As you can see, this is a lot less code than the previous example. While creating your own class to implement the WindowListener
interface is handy when you need to override several methods in that interface, the adapter is nice for situations like this, where you're implementing only one method.
3) A simple way to handle the window closing event
Now, if you want a really fast way of handling this you can just tell your JFrame how to handle this event with a default operation. You do this with a simple method call on your JFrame object, like this:
myJFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Of course that is a lot simpler, and it's very useful when you're creating prototypes, but you probably don't want to do this in production applications. You'll usually want to warn the user before shutting down the entire application.