1

what is different between Dispose_On_Close and Exit_On_Close in setDefaultCloseOperation method in JFrame class?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Mohammadreza Khatami
  • 1,444
  • 2
  • 13
  • 27

2 Answers2

6

As their name implies, dispose on close disposes the window when it's closed while exit on close exits the JVM on window close. If the window is a JFrame and if it is the last window showing and if there are no non-daemon threads running, they'll both do the same thing -- exit the jvm. Per the API:

When the last displayable window within the Java virtual machine (VM) is disposed of, the VM may terminate.

I generally prefer to use dispose on close because of this.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
3

EXIT_ON_CLOSE ends the complete thread by calling System.exit(0). DISPOSE_ON_CLOSE only closes the jframe, the thread runs on until it's terminated by something else. You can test this with a program that has 2 JFrames with the 2 different close operations. If you close the one with EXIT_ON_CLOSE both Frames will close, but if you close the one with DISPOSE_ON_CLOSE set as close operation, only this one will disappear and the other one will stay.

Gumbo
  • 1,716
  • 1
  • 15
  • 22