Search code examples
javaswingjframewindowlistener

Do something before window closes after user presses [x]


I'm building an instant messenger application.I need to add an action event on the default closing button of the Swing JFrame(The little "x").
When the client presses the X button,I need to tell the server that he goes offline and only after that action occures I have to close the window.I can't seem to find how to get an action listener on the default button.


Solution

  • take a look of this may its helps you. Closing an Application
    You can give your own implementation if some one press close button.

    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    
    frame.addWindowListener( new WindowAdapter()
     {
       public void windowClosing(WindowEvent e)
        {
          // Here you can give your own implementation according to you.
         }
      });