-1

I was looking at different answers for this kind of question but they didn't meet my specifications. I have a class app1.java. It has the public static void main function in which there is a JOptionPane.showConfirmDialog object. On clicking yes, I want to run another java file called "app2.java" and close the current running applocation( i.e. app1.java). I tried this code:

int response = JOptionPane.showConfirmDialog(null, "Do you want to restart?", "Restart Game?",
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (response == JOptionPane.NO_OPTION) {
              System.exit(0);
                } 
            else if (response == JOptionPane.YES_OPTION) {
              new app2();
                } 
            else if (response == JOptionPane.CLOSED_OPTION) {
              System.exit(0);
                }

The problem is that on clicking yes, a new window opens but the former one does not close. I cannot try System.exit as it closes both the windows. What should be done?

Ankit Shubham
  • 2,989
  • 2
  • 36
  • 61
  • check this http://stackoverflow.com/questions/9572931/java-swing-application-close-one-window-and-open-another-when-button-is-clicked – ziLk Apr 22 '16 at 14:29
  • 1
    And please read this: [Multiple JFrames](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-or-bad-practice/9554657) before you annoy your application users too much – Hovercraft Full Of Eels Apr 22 '16 at 14:42
  • Note that `new app2()` does not necessarily do the same thing as executing app2, assuming that app2 actually is an application - that is, assuming that there is a main() method in app2. – FredK Apr 22 '16 at 15:22

2 Answers2

2

If you are using JFrame then you can use dispose() method to close the current application and make it available for garbage collection.

jframeObj.dispose();
new app2();
Vitthal Kavitake
  • 879
  • 5
  • 18
0

Just close the frame, but not the whole application:

Jframe jframe = new JFrame();
jframe.dispose();
new app2();
Marcel
  • 1,509
  • 1
  • 17
  • 39