Java Glossary : close

CMP home Java glossary home Menu no menu Last updated 2004-06-28 by Roedy Green ©1996-2004 Canadian Mind Products

Java definitions: 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

You are here : home : Java Glossary : C words : close.

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:

this.setDefaultCloseOperation ( JFrame. DO_NOTHING_ON_CLOSE );
// what happens when user closes the JFrame.
WindowListener windowListener = new WindowAdapter()
   {
   // anonymous WindowAdapter class
   public void windowClosing ( WindowEvent w )
      {
      // Whatever application code you want to do on close, e.g.
      rememberLocation( MyFrame.this.getX(), MyFrame.this.getY() );

      // Whatever code you want to actually close the JFrame, e.g.
      MyFrame.this.setVisible( false );
      MyFrame.this.dispose();
      } // end windowClosing
   };// end anonymous class
this.addWindowListener( windowListener );

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 logo
CMP_home
home
Canadian Mind Products CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[24.87.56.253]
Your IP:[80.134.30.163]
You are visitor number 4244.
Please send errors, omissions and suggestions
to improve this page to Roedy Green.
You can get a fresh copy of this page from: or possibly from your local J: drive mirror:
http://mindprod.com/jgloss/close.html J:\mindprod\jgloss\close.html