setDefaultCloseOperation not working in Swing

2019-07-15 16:26发布

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?

2条回答
时光不老,我们不散
2楼-- · 2019-07-15 17:00

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

查看更多
地球回转人心会变
3楼-- · 2019-07-15 17:04

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)
  {
  }

});
查看更多
登录 后发表回答