Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


 

Java Tutorials


 

 

Struts Tutorials

Struts Resources

Visit Forum! Post Questions!
Jobs At RoseIndia.net!

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) 
Ask programming questions?

 

 

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name

 

 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright 2007. All rights reserved.

[an error occurred while processing this directive]