4

I need to stop the default operation of window being closed when red x mark is clicked on the swing window. I am using the JDialog and adding WindowsListener to it to capture the WindowClosing event, there I decide whether to dispose JDialog or to not dispose it, I am also setting the following:

setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);

But still when I click on the red x mark, the window closes. Any ideas?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Gaurav
  • 1,570
  • 4
  • 20
  • 25

2 Answers2

0

You can try creating a WindowListener and do nothing when the close buttion is clicked.

jdialog.addWindowListener(new WindowAdapter() 
{
  public void windowClosed(WindowEvent e)
  {   
  }

  public void windowClosing(WindowEvent e)
  {
  }

});
ganesshkumar
  • 1,317
  • 1
  • 16
  • 35
  • Yes, this I am doing, but by default the behavior of the closing of the window is happening. To override the default behavior we need to do setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);, which I am doing, but it is still closing – Gaurav Mar 29 '12 at 12:18
  • the function you mentioned will work perfectly for jFrame. I'm not sure will it work for jDialog. Check the same. – ganesshkumar Mar 29 '12 at 12:31
  • 1
    -1 adding a do-nothing listener certainly has exactly ... zero effect (dialog or frame doesn't matter) – kleopatra Mar 29 '12 at 12:55
  • @Kleopatra It has zero effect. At the same time it wont close the window also. – ganesshkumar Mar 29 '12 at 12:58
  • the frame/dialog is closed by default :-) – kleopatra Mar 29 '12 at 13:12
  • @kleopatra it actually did an effect, I just needed to add an empty window listener and somehow it worked. `addWindowListener(new WindowAdapter(){});` – Polyana Fontes Oct 10 '21 at 03:01
0

Adding Window listener to the JDialog gave me the power to handle the window actions and I works fine in my application.

ganesshkumar
  • 1,317
  • 1
  • 16
  • 35
  • ehh ... this answer is very vague: no doubt it _can_ be done, the OP wants to know _how_ exactly to setup the dialog and _what_ exactly to do in the listener :-) – kleopatra Mar 29 '12 at 13:10