You may, if you wish, create a subclass of the
ApplicationClosingWindowListener
that alters or supplements the
behavior of the superclass. For example, suppose that you want your users to be
told when Java is about to exit from an application. Then, you could
define the following PausingWindowListener
subclass. There is no
need to define most of the methods required by the WindowListener
interface, because all but one of those methods is defined adequately in
the superclass. You do, however, wish to define windowClosing
such
that it pops up a message window, arranged by the JOptionPane
class
method, showMessageDialog
. After you click your acknowledgement,
windowClosing
calls the windowClosing
method in the
ApplicationClosingWindowListener
superclass, which shuts down the
application.
import javax.swing.*; import java.awt.event.*; public class PausingWindowListener extends ApplicationClosingWindowListener { private MovieApplication application; public PausingWindowListener (MovieApplication a) { application = a; } public void windowClosing(WindowEvent e) { JOptionPane.showMessageDialog(application, "Shutting down..."); super.windowClosing(e); } }