Ask Questions?

View Latest Questions

Advertisement


 
 

JFrame Close Box
Posted on: July 22, 2006 at 12:00 AM
Terminating the program when the close box is clicked

Java: JFrame Close Box

Terminating the program when the close box is clicked

A JFrame is created (in Windows) with three control boxes at the right end of the window title bar -- minimize, smaller/larger, and close (or whatever is appropriate for the system you are using). The first two have default actions that you wouldn't normally change. However, the close box only closes the window by default, but doesn't terminate the program. You may then be in the bizarre position of having a program running, but with no GUI to interact with it. The default action was not wisely chosen, but there it is.

It's common for small programs to simply terminate the program when the close box is clicked. There are two ways of handling this. The easiest is to use setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE), which closes all windows and terminates the program.

   myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

To do some processing when the window closes

Sometimes you need to do some processing when the window closes. For example, it's typical to check to see if there is any unsaved work and ask the user if it should be saved. Or you want to remember the size and position of the window so you can open it the same way. To do additional processing (or for pre Java 2), override the addWindowListener() method in a java.awt.event.WindowAdapter.

myframe.addWindowListener(new java.awt.event.WindowAdapter() {
    public void windowClosing(WindowEvent winEvt) {
        // Perhaps ask user if they want to save any unsaved files first.
        System.exit(0); 
    }
});

The java.awt.event.WindowEvent.getWindow() method can be called to get the window that is being closed, but this is rarely necessary.

Terminating when the last window is closed

If you have many open windows, you must declare a (very likely static) variable to keep track of the number of open windows, then call System.exit(0) only when the last window is closed.

myframe.addWindowListener(new java.awt.event.WindowAdapter() {
    public void windowClosing(java.awt.event.WindowEvent e) {
        if (openWindows == 1) {
            System.exit(0);  // Terminate when the last window is closed.
        }
        g_openWindows--;
    }
});

Listening to all window events

Generally it's easier to override individual methods in the WindowAdapter convenience class (as above). If you want to handle all window events, write the following methods to implement the java.awt.event.WindowListener interface. For example, on a window close to check for a condition and then possibly not close the window, you need to add a full window listener (not just override a WindowAdapter method.

void windowActivated(WindowEvent e)
void windowClosed(WindowEvent e)
void windowClosing(WindowEvent e) 
void windowDeactivated(WindowEvent e) 
void windowDeiconified(WindowEvent e) 
void windowIconified(WindowEvent e) 
void windowOpened(WindowEvent e) 
Copyleft 2006 Fred Swartz MIT License
Advertisement


DMCA.com