aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Stop JFrames/JDesktopPanes from closing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Stop JFrames/JDesktopPanes from closing" Watch "Stop JFrames/JDesktopPanes from closing" New topic
Author

Stop JFrames/JDesktopPanes from closing

Patrick ODonnell
Ranch Hand

Joined: Nov 28, 2001
Posts: 31
Hi all,
I was wondering if it is possible to stop a JFrame or a JDesktopPane from close when the close icon(in the top right hand corner) is pressed??
I know a JInternalFrame can be closed using an InternalFrameListener and you can add a windowListener as follows but what goes inside??
Or is this the correct way of doing this??
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
// What code goes in here
}
});
Any help would be great!!
Thanks,
Patrick.
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
for JFrame its easy.
type this:
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
Patrick ODonnell
Ranch Hand

Joined: Nov 28, 2001
Posts: 31
Thanks that worked lovely!!
Can't believe it was that easy
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
by the way Patrick, pay attention that if u dont write anything for the close operation the default is to HIDE the frame, not CLOSE it, so it never really ends.
to make it close u need to put this:
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Stop JFrames/JDesktopPanes from closing