COMP 310

Setting the Behavior When Closing a JFrame 

    Current Home  Java Resources  Eclipse Resources

There are a number of techniques that one can use to close a frame in Java.   Each has its advantages and disadvantages.  Three techniques are presented below in general decreasing preference order:

JFrame.defaultCloseOperation(int option)

This is the simplest technique.   All you need to do is to call the defaultClostOperation method of a JFrame, supplying the desired option value.

The possibilities for the option value are:

The advantage of the technique is that it is very simple and easy, but it does not allow much flexibility to do any custom operations during the frame closing.

Example:

public class MyFrame extends JFrame {
    public MyFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    ...
}

For dynamically configurable frame closing behavior, supply the option value to the constructor of the frame.   This would be done by the controller, which would instantiate the frame with either an EXIT_ON_CLOSE option if it was an application or a DO_NOTHING_ON_CLOSE or DISPOSE_ON_CLOSE if it were an applet.   

 

java.awt.Window.addWindowListener(java.awt.event.WindowListener l)

(Note that javax.swing.JFrame is a subclass of java.awt.Window and thus inherits this method.)

In this technique a WindowListener interface implentation is added to the frame, where the listener has a method, windowClosing(), that is called when the frame is closed.

In practice, on overrides the windowClosing() method of WindowAdapter, a no-op implementation of WindowListener.   This way, one doesn't have to worry about all the other methods of the WindowListener.

For example:

public class MyFrame extends JFrame {
    public MyFrame() {
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent e) {
                System.exit(0);
            }
        });
    }
    
    ...
}

This technique has the advantage that any sort of custom processing can be done when the frame is closed.   The frame can be constructed with a command that the windowClosing method calls or with an complete WindowListener object, thus enabling dynamic configuration of the frame closing behavior.   This is very useful when you want to run the same frame as either a regular application or as an applet -- the controller will instantiate the frame with the proper closing behavior.

The disadvantage of this technique is the larger code required than the first technique for standard window closing behaviors.

 

java.awt.Window.processWindowEvent(java.awt.event.WindowEvent e)

(Note that javax.swing.JFrame is a subclass of java.awt.Window and thus inherits this method.)

In this technique, the processWindowEvent method, which is called when the frame is closed, is overriden to provide the desired behavior.    The supplied WindowEvent parameter is tested to see if it is the event for the window is being closed and if so, the desired behavior is executed.

For example:

public class MyFrame extends JFrame {
    public MyFrame() {
        protected void processWindowEvent(WindowEvent e) {
            super.processWindowEvent(e);
            if(e.getID() == WindowEvent.WINDOW_CLOSING) {
                System.exit(0);
            }
        }
    }
    ...
}

For dynamic behavior, a command supplied to the constructor of the frame can be run when the WINDOW_CLOSING event is detected.

The advantage of this technique is that an entire object to handle the frame closing needs to be constructed but at the expense of a more complicated logical process.

 

 


© 2017 by Stephen Wong