Java Basics

Window close operation

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE), a window listener, or nothing?

Window listeners. When a window of a program is closed, you typically want to check to see if there is any unsaved data, and if so, ask the user if they want to save it. To do this you need to add a window listener.

setDefaultCloseOperation. Although adding a window listener is the right way to handle the close box click in a real program, simple programs often don't have any state that needs to be saved, so I've used the common shortcut of terminating the program when the window is closed. The default action of closing a window just makes it invisible, but the program continues to run, making it hard to stop the program, so it's essential to have either this or a listener.

When the tutorial gets to the point where there is actually a state to be changed, window listeners will be introduced.