In my java swing application am having a Jframe and Jlabel for displaying current time.
here am using a thread for displaying time in jlablel which is added to the frame.my doubt is that when i dispose the jframe what will happen to the thread whether its running or stopped.
In my java swing application am having a Jframe and Jlabel for displaying current
Share
If you have NOT marked your thread as daemon by calling
yourThread.setDaemon(true), it will keep running even if main thread in your application has finished. Remember you have to callsetDaemonbefore starting the thread.Refer my answer to some previous question for details.
The correct way in your case, I believe, would be you maintain a ‘stop’ flag which is watched by your timer thread. Timer thread should exit on reading this flag as ‘false’. You can add a WindowListener to your jframe and on the window closed event set the ‘stop’ flag to true
Heres example code for what I am suggesting :