Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dispose() vs setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Tags:

java

swing


I have been searching around quite a bit about this topic but never got any satisfaction about any answers so ended up asking a question here :). Anyways, from this similar post it was clear that dispose() does not terminate all the processes and still has remains in the task manager(the AWT process is not terminated completely) even after the JFrame is closed.

So, my question is :-

  1. does this.dispose() cleans off every child process after disposing the JFrame? or setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); does this job instead.
  2. Which one will be good to go with performance-wise, if I am developing applications for low spec PCs

Thanks, any suggestion on this is appreciated.

like image 539
mustangDC Avatar asked Jan 02 '26 07:01

mustangDC


1 Answers

To answer 1.

dispose() on a JFrame will destroy the window and have the operating system clean up after it, if its the only JFrame left, the Java VM might terminate. So, in summary, it will close the window, and if the JFrame is the last remaining window of your application, it might (or might not) terminate your VM.

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) however, will call System.exit() when the JFrame is closed, terminating the VM regardless of there being any other active JFrames in your app.

So, they are two different things :-)

To answer your second question, you could test it. But honestly, I wouldn't be to worried about this.

like image 60
JustDanyul Avatar answered Jan 03 '26 21:01

JustDanyul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!