tags:

views:

97

answers:

3

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?

+7  A: 

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.

Joachim Sauer
@Joachim again, if I want to do something I should decide about it, which is if I have other tasks to perform I should personally take care of it and define this desired behaviour.
There is nothing we can do
@Knowing: uhm ... yes, and? That's exactly what the current state provides: It has some defined default behavior (for which I just argued that it's useful) and if you want anything different or in addition to that, then you will have to write that code.
Joachim Sauer
@Joachim I agree for now. +1
There is nothing we can do
+8  A: 

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);
nos
+1 for the first sentence.
Stephen C
A: 
myJFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

Dispose is almost always preferred. If you explicitly say EXIT_ON_CLOSE and you ever want to display more than a single window, you will need to modify your code to use DISPOSE_ON_CLOSE instead. Not a huge deal, of course, just seems a better choice.

In my opinion, the DISPOSE_ON_CLOSE should have been the default, not HIDE_ON_CLOSE. Oh well, that's what we've got. EXIT_ON_CLOSE should probably be avoided for the same reason that calling System.exit(0) should be avoided in a window listener (though there's nothing wrong with it per se, it's just not very future flexible).

Even better, it's almost always preferred to attach a WindowListener to your frame and set the default close to JFrame.DO_NOTHING_ON_CLOSE. This way, you control when your application shuts down, including a prompt to the user to save any work, etc.

taftster