close : Java Glossary

*0-9ABCDEFGHIJKLMNOPQRSTUVWXYZ (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 (Advanced Windowing Toolkit) 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.


This page is posted
on the web at:

http://mindprod.com/jgloss/close.html

Optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\close.html
Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

IP:[65.110.21.43]
Your face IP:[18.119.213.235]
You are visitor number