Why when I click on the x button to close the window in a Java application only the window dissapears and the applicaton is still running.
I've read so many times that java designers tried to cater Java behaviour for the most common needs of programmers and save they precious time and so on and so on. What's more common case than closing app when I click on a X button?
Whatever the reason is, it really doesn't matter as long as you're aware of how it works and how to make your program behave the way you want.
At a guess though: One app can have several windows, exiting the app when one of them is closed doesn't sound very smart. Keeping track of how many windows are open/closed/hidden/not yet shown and so fort to be able to exit when the last windows is closed might have been too much work/too many edge cases etc. Thus it's up to us, programmers, when we want our app to exit.
In any case, if you want your app to exit when a window(JFrame) is closed, you can just tell it to do so:
myJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Many applications have more than one window open at a time and closing a single window should definitely not shut down the whole application.
Also, most application want to do a bit more than a simple System.exit(0); when the quit. They might want to ask the user to save some files (or save them automatically), they might want to clean up some temporary files or notify some remote host of their shutdown or something like that.
In effect it's very unlikely that the only effect of closing a window is ending the JVM.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With