Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Stop JFrames/JDesktopPanes from closing

 
Patrick ODonnell
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for JFrame its easy.
type this:
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
 
Patrick ODonnell
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks that worked lovely!!
Can't believe it was that easy
 
Roy Ben Ami
Ranch Hand
Posts: 732
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic