How to Close a Window in Java

Method 1 of 2:

Using javax.swing.JFrame

  1. How to Close a Window in Java Picture 1
    Obtain an instance of a JFrame, or create a new one.
  2. How to Close a Window in Java Picture 2
    Set default close operation. Default close operation is set using the setter method inside the JFrame class setDefaultCloseOperation that determines what happens when the close button is clicked and takes the following parameters:
    1. WindowConstants.EXIT_ON_CLOSE - Closes the frame and terminates the execution of the program.
    2. WindowConstants.DISPOSE_ON_CLOSE - Closes the frame and does not necessarily terminate the execution of the program.
    3. WindowConstants.HIDE_ON_CLOSE - Makes the frame appear like it closed by setting its visibility property to false. The difference between HIDE_ON_CLOSE and DISPOSE_ON_CLOSE is that the latter releases all of the resources used by the frame and its components.
    4. WindowConstants.DO_NOTHING_ON_CLOSE - Does nothing when the close button is pressed. Useful if you wish to, for example, display a confirmation dialog before the window is closed. You can do that by adding a WindowListener to the frame and overriding windowClosing method. Example of the custom close operation:
      frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { // Ask for confirmation before terminating the program. int option = JOptionPane.showConfirmDialog( frame, "Are you sure you want to close the application?", "Close Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if (option == JOptionPane.YES_OPTION) { System.exit(0); } } }); 
Method 2 of 2:

Using java.awt.Frame

  1. How to Close a Window in Java Picture 3
    Obtain an instance of a Frame, or create a new one.
  2. How to Close a Window in Java Picture 4
    Add window listener. Call addWindowListener method on the instance. The required argument is WindowListener. You can either implement every method of the WindowListener interface or override only the methods you need from WindowAdapter class.
  3. How to Close a Window in Java Picture 5
    Handle window closing event. Implement windowClosing method from WindowListener interface or override it from WindowAdapter class. There are two ways of closing a window:
    1. Dispose the window after the close button is clicked:
        1. Call dispose method inside windowClosing method.
      frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { // Dispose the window after the close button is clicked. dispose(); } }); 
    2. Terminate the program after the close button is clicked:
        1. Call System.exit method inside windowClosing method.
      frame.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { // Terminate the program after the close button is clicked. System.exit(0); } }); 
3.4 ★ | 7 Vote

May be interested

  • How to Compare Two Dates in JavaPhoto of How to Compare Two Dates in Java
    there are various ways to compare java dates. internally, a date is represented as a (long) point in time -- the number of milliseconds that have elapsed since january 1 1970. in java, date is an object, which means it includes multiple...
  • How to Update JavaPhoto of How to Update Java
    this wikihow teaches you how to update java on your computer. although java will usually update itself when possible, you can use java's update feature to force an available update on windows and mac computers. open start . click the...
  • How to Determine Java VersionPhoto of How to Determine Java Version
    multiple copies of java can be installed on a single computer, and, if you have more than one browser, every one of them could be using a different version or none at all. you can determine your java version by verifying it on the java...
  • How to Print an Array in JavaPhoto of How to Print an Array in Java
    if you are working on java and have an array with a large amount of data, you may want to print certain elements in order to view them conveniently. there are multiple ways you can print arrays in java and the examples given below will...
  • How to Write Your First Program in JavaPhoto of How to Write Your First Program in Java
    java is an object-oriented programming language created in 1995 by james gosling, which means that it represents concepts as 'objects' with 'fields' (which are attributes that describe the object) and 'methods' (actions that the object can...
  • How to Create an Executable File from EclipsePhoto of How to Create an Executable File from Eclipse
    after having completed your project in eclipse, your next goal will be to create a runnable version of your project. the simplest and most standard process for running a java project is to run an executable file (.exe). in this guide,...