Package examples

Class ExitExample1


public class ExitExample1 extends Application
Demonstrate the use of an ExitListener.

This class adds an Application.ExitListener that asks the user to confirm exiting the application. The ExitListener is defined like this:

class MaybeExit implements Application.ExitListener {
    public boolean canExit(EventObject e) {
        Object source = (e != null) ? e.getSource() : null;
        Component owner = (source instanceof Component) ? (Component)source : null;
        int option = JOptionPane.showConfirmDialog(owner, "Really Exit?");
        return option == JOptionPane.YES_OPTION;
    }
    public void willExit(EventObject e) { }
}
 
When the user attempts to close the window, Application.exit is called by JFrame's WindowListener. The exit method checks the ExitListener.canExit methods and aborts the attempt to exit if any of them return false.