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 install Java on a Raspberry PiHow to install Java on a Raspberry Pi
    there are two different java implementations, oracle java and openjdk. this tutorial explains how to install java (openjdk) on a raspberry pi with the latest raspbian operating system running on it.
  • How to fix the error does not install JavaHow to fix the error does not install Java
    more and more applications and websites require java installation before use. unfortunately, sometimes you can't install java or install it, but it doesn't work. the article will provide ways to fix errors that do not install java on your computer.
  • How to Tune a Java Virtual Machine (JVM)How to Tune a Java Virtual Machine (JVM)
    the java virtual machine (jvm) runs your java programs. sometimes the default configuration that the jvm comes with may not be the most efficient for your program.
  • What is the difference between Go and Java?What is the difference between Go and Java?
    is golang better than java? is golang harder than java? can golang replace java? this comparison will give you the answer.
  • How to Fix JavaHow to Fix Java
    java is a computing platform that allows you to play games and view videos on your computer. you can tell that your computer is having problems with java if you see java errors appear when you try to run a program or visit a website that...
  • How to Compile a Java ProgramHow to Compile a Java Program
    this wikihow teaches you how to turn your java source code into an executable app using a local and online compiler. if you're using a computer, the most common way to compile java code is using java software development kit (java sdk)...
  • Oracle wants to turn Java EE into fully open sourceOracle wants to turn Java EE into fully open source
    this week, oracle announced plans to move java ee project management to an open source platform, like apache or eclipse.
  • Write and run Java code on the computer for the first timeWrite and run Java code on the computer for the first time
    after you've installed java and set up a path on your computer, you need to know how to run a simple java code on your computer. in this article, we will try to write the first java program, as well as the commands to run this program in cmd.
  • Download Java Runtime Environment 8-build-251Download Java Runtime Environment 8-build-251
    java runtime environment (jre) is a software class that provides the services needed to execute java applications. it is an essential component of the java development kit (jdk) and contains all the tools needed to run various java-based software programs.
  • How to Enable JavaHow to Enable Java
    java is a programming language and platform commonly used in a number of websites and applications. when java is not enabled, you can experience difficulty with viewing or using certain websites and applications. to use java, you must have...