10

Is there any difference between the two. I was reading an article ( http://www.javalobby.org/java/forums/t17933 ) about that you should always use

System.exit(0);

Currently I use

JFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

The article says that even for a Java Swing Application you should add a listener WindowAdapter and and call System.exit() inside its method windowClosing(WindowEvent e).

Is there any difference? Is one method better then the other?

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
Halfwarr
  • 7,853
  • 6
  • 33
  • 51

2 Answers2

13

If you look at the JFrame code, it does:

 protected void processWindowEvent(WindowEvent e) {
        super.processWindowEvent(e);

        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
            switch(defaultCloseOperation) {
              ...
          case EXIT_ON_CLOSE:
                  // This needs to match the checkExit call in
                  // setDefaultCloseOperation
        System.exit(0);
        break;
            }
        }
    }

So, it's exactly the same thing. I would just set EXIT_ON_CLOSE if that's what you want it to do.

Reverend Gonzo
  • 39,701
  • 6
  • 59
  • 77
0

Well, considering System.exit(0) is in the JFrame coding, either would work.