javaswingwindowlistener

Closing a java program properly when JDialog is the main window


I have a JDialog as the main window in my application (originally it was a JFrame but it showed in the taskbar which I didn't want).

Currently I am doing:

setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

and when I click an exit button:

frame.dispose();

But the process still seems to hang around in the background

JFrame had JFrame.EXIT_ON_CLOSE which seemed to do what I wanted.

How can I close my application properly?


Solution

  • You can add

      System.exit(0);
    

    where you want the program to end, maybe immediately after the dispose() line.