Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java JFrame and program flow

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

like image 770
EscapistGR Avatar asked Apr 21 '26 16:04

EscapistGR


2 Answers

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);
like image 86
Hovercraft Full Of Eels Avatar answered Apr 23 '26 07:04

Hovercraft Full Of Eels


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.

like image 32
John Schneider Avatar answered Apr 23 '26 08:04

John Schneider



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!