I would like to have a button in my window such that if I click it (button) the window is closed.
I found out that I can close a window in the following way:
referenceToTheFrame.hide(); //hides the frame from view
refToTheFrame.dispose(); //disposes the frame from memory
But if I do this way, compiler complains:
Note: myProgram.java uses or overrides a deprecated API
Note: Recompile with -Xlint:deprication for details.
Do I do something unsafe?
The recommended way is to use:
The
hidemethod is deprecated and should not longer be used. (Although internallysetVisiblewill callhideorshow)Additionally if you want to dispose the frame, you have to call
disposemanually. (In case you need the window-closed event for example) A call tosetDefaultCloseOperationdoesn’t help you here, because it only affects the behavior after a click on the close button from the system menu.See the Swing documentation for further information.