close : Java Glossary
home C words local find no local find frame, full screen Google search web for topic jump to footer translate with Babelfish by Roedy Green ©1996-2008 Canadian Mind Products
Go to : punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all)
close
To cut to the chase of how you normally close JFrames, see shorthand.

You may have noticed there is no Frame.close() or JFrame.close() method. How then do you close a JFrame? Closing a JFrame is difficult to get your head around mainly because there are so many variants on how you can do it. You can:

You can close the JFrame:

When the user clicks the close X, that does not make your JFrame object mysteriously disappear or force it to close, or go invisible. All it does is send your JFrame a WindowClosing event. Your JFrame is free to ignore the event, close the JFrame in any of the four usual ways, or do something else entirely.

The event in no way closes your JFrame or changes its state. It is totally up to you what happens. Receiving a WindowClosingEvent is not an eviction notice; it is just notification the user idly clicked an X. It does not mean the JFrame is closing or that it has to close, just that the user would like it to.

Final Close

When you want to get rid of JFrame entirely and never want to use it again:

Temporary Close

If you just want to hide the JFrame for a short time, ready to bring it back without any loss of data, use setVisible( false). To bring it back, use setVisible( true ).

Cold Storage Close

If you want to hide the JFrame for an extended period of time, storing it in a more compact form without its associated GUI overhead, ready to bring it back somewhat more slowly, but without any loss of data, use dispose(). To bring it back, use setVisible( true ).

WindowClosingEvent

To arrange for Swing to notify you when the user clicks X, you need code like this to capture WindowClosing events:

DO_NOTHING_ON_CLOSE is a bit of a misnomer. It should have been called USE_EVENT_HANDLER_ON_CLOSE. It means Swing does nothing to handle the events automatically, but hand the problem over to you at your windowClosing method.

If you put no code at all in your windowClosing method, the event would be ignored. The user could click the X, but nothing would happen. You are free to put any code your heart desires in there, whatever you want to happen when the user clicks X.

Shorthand WindowClosingEvent Handlers

There are three shortcuts to writing WindowClosing event handlers. With these shortcuts, you have no opportunity to add any of your own application code. You have no opportunity to add any code to disconnect the JFrame from Collections or Containers.

AWT and Frame

Everything I said about JFrames also applies to Frames. However, there is no Frame.setDefaultCloseOperation. You can’t use shortcuts. Unless you want to ignore WindowClosing events you must write a windowClosing event handler.

Caveats

If you don’t set up a WindowClosing method use a
this.setDefaultCloseOperation ( JFrame.DISPOSE_ON_CLOSE );
It will default to HIDE_ON_CLOSE which will leave JFrames lying about un-garbage collected.

Be careful not to cast adrift an undisposed JFrame with no references to it. It can never be garbage collected.

To iconify (minimise), maximize etc. programmatically see Frame.setExtendedState( Frame.ICONIFIED ); Other constants you can use include Frame.MAXIMIZED_HORIZ, Frame.MAXIMIZED_VERT, Frame.MAXIMIZED_BOTH, and Frame.NORMAL. Prior to Java 1.14 you had to use Frame.setState( Frame.ICONIFIED );

Oddly, for JInternalFrames, you have to use a different technique entirely using the DeskTopManager class.


CMP_homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.16] The information on this page is for non-military use only.
You are visitor number 56,007. Military use includes use by defence contractors.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/Mindprod website mirror)
http://mindprod.com/jgloss/close.html J:\mindprod\jgloss\close.html