Responding to Window-Closing Events
by, 04-26-2012 at 08:51 PM (641 Views)
When a frame onscreen is closed by a user, frame gets hidden by defauly. Though it is visible, still the frame is present and it might be made visible by the program once again. If different behavior is needed on your partm then either get registered the window listener which controls the window closings or default close behavior could be specified by the help of method setDefaultCloseOperation. Both of them could also be done.
Given values shall be there for setDefaultCloseOperation argument. Among them, first three have been defined and detailed in WindowConstants interface (which is implemented by the help of JDialog, JInternalPane and JFrame.)
When a request is made by the user to close the windoe, nothing needs to be done. Instead a window listener shall be used by the program which performs various actions in its method windowClosing.
HIDE_ON_CLOSE (the default for JDialog and JFrame)
Window shall be made hidden when it is closed by user. Window will be erased from the screen but would be left displayable.
DISPOSE_ON_CLOSE (the default for JInternalFrame)
When user close window, hide or dispose off it. It will remove out the window and also would free any used resource.
EXIT_ON_CLOSE (defined in the JFrame class)
Use System.exit(0) to close the application. This has been just recommended for applications. If it is used within applet, throw a SecurityException.