What is the difference between these two methods – System.exit() and JFrame.dispose()?
If we want to close a Java Swing application when a button is clicked, which method should I use?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
System.exit();causes the Java VM to terminate completely.JFrame.dispose();causes theJFramewindow to be destroyed and cleaned up by the operating system. According to the documentation, this can cause the Java VM to terminate if there are no other Windows available, but this should really just be seen as a side effect rather than the norm.The one you choose really depends on your situation. If you want to terminate everything in the current Java VM, you should use
System.exit()and everything will be cleaned up. If you only want to destroy the current window, with the side effect that it will close the Java VM if this is the only window, then useJFrame.dispose().