'processWindowEvent in mac

I have a java swing application which has a processWindowEvent method. below is the snippet

@Override
  protected void processWindowEvent(WindowEvent e) {
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
        //exit application with proper error message
    }
  }

Now when my swing application is launched in windows.

  1. close with the cross in swing UI. ==> proper error message is shown
  2. close the application from taskbar ==>proper error message is shown

but now if the same step is done in mac.

  1. close with the cross in swing UI. ==> proper error message is shown
  2. close the application from taskbar ==>does not come inside above method. So no proper message.

I wanted to know what is the default method which will be called in mac when java swing app is closed from taskbar(the dock)



Solution 1:[1]

To catch window close event you need to add WindowListener and override method windowClosing. Use this code:

    JFrame frame = new JFrame(...);
    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

    frame.addWindowListener( new WindowAdapter()
    {
        public void windowClosing(WindowEvent e)
        {
            JFrame frame = (JFrame)e.getSource();
    ....................
}
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jay Smith