When i create a simple program with a JFrame ,from the moment the setVisible method is true, no matter what i do with the frame, the program doesn't exit.Could someone explain me the flow? Thanks
When you call myJFrame.setVisible(true) you are creating a non-daemon Swing event thread which prevents the program from ending until this thread and all non-daemon threads have ended. Note, if you want the program to end when the JFrame is closed, call
myJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
When a JFrame is open, I know that the program will continue updating the JFrame even if it is invisible, to do things such as check if it is visible again, and also to check if the screen needs updating. If you want to close the program when a JFrame closes, you can use:
JFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
which doesn't update the thread that checks for the visibility, it just stops the program when the JFrame stops being visible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With