3

I was taught to add this to always add this to the run method of jframe, but it doesn't change anything compared to not using it.

What I want to know is, what happens when you leave this out?

Thanks

@Override
public void run() {
    frame = new JFrame(title);
    frame.setPreferredSize(new Dimension(400, 200));
    //frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    createComponents(frame.getContentPane());
    frame.pack();
    frame.setVisible(true);
}
SJ19
  • 1,933
  • 6
  • 35
  • 68

3 Answers3

4

The process of your app will still remain in memory.

This is from the official documentation:

-DO_NOTHING_ON_CLOSE (defined in WindowConstants): Don't do anything; require the program to handle the operation in the windowClosing method of a registered WindowListener object.

-HIDE_ON_CLOSE (defined in WindowConstants): Automatically hide the frame after invoking any registered WindowListener objects.

-DISPOSE_ON_CLOSE (defined in WindowConstants): Automatically hide and dispose the frame after invoking any registered WindowListener objects.

-EXIT_ON_CLOSE (defined in JFrame): Exit the application using the System exit method. Use this only in applications.

Kristian Vukusic
  • 3,284
  • 6
  • 30
  • 46
0

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE) is used to close your JFrame as well as Java process. Test it with a JFrame sample program without the setDefaultCloseOperaton. Close your jframe by clicking the windows close. Check your task manager/top command , you will find the java process will be still running.

Shriram
  • 4,343
  • 8
  • 37
  • 64
0

I know this is an old question, but what I was taught to do was create an inner class in the class that extends JFrame.

The inner class should extend WindowAdapter, and you should override the windowClosing method, I usually just put System.exit(0); in it.

Then in the constructor of your JFrame class, add this line: this.addWindowListener(new MyWindowAdapterClass());

Umar Osman
  • 21
  • 9