setDefaultCloseOperation not working in Swing

☆樱花仙子☆ 提交于 2020-01-04 13:31:29

问题


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?


回答1:


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




回答2:


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

});


来源:https://stackoverflow.com/questions/9924835/setdefaultcloseoperation-not-working-in-swing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!