0

I am new to Java, newer to swing. In a project I am trying to dispose a frame when a task is completed. I have a static boolean variable JobDone initially set to false in the class (FinalOutput) where the main task is performed and after the task gets completed it is set to true. I searched about checking something periodically in java and I found it can be achieved through Timer. Now based on this variable I'm trying to dispose the frame using a Timer.

Below is what I tried: This code in main function of the frame (ProgressBarForm) to be disposed.

                ProgressBarForm obj = new ProgressBarForm(); 
                Timer t = new Timer();

                t.scheduleAtFixedRate(new TimerTask(){
                @Override
                public void run(){ 
                    if(FinalOutput.isJobDone())
                        obj.dispose();
                }}, 100, 100);

But the frame is not getting disposed after the job gets completed. Kindly help me through this. Thanks in advance.

Diksha Dhote
  • 1
  • 1
  • 4
  • did you debug the return value of `FinalOutput.isJobDone()`? –  Jul 04 '18 at 09:09
  • No. But the the variable is set to true just after the statement where final output gets produced. I am getting the output file as expected. – Diksha Dhote Jul 04 '18 at 09:11
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) See also [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Jul 04 '18 at 09:14
  • I assume this is because you shouldnt call `dispose()` from some random other thread. Just follow the guidance given in the duplicated question. In case that does **not** solve your problem, feel free to A) update your question indicating that you tried that B) ping me, then I will gladly reopen this question. – GhostCat Jul 04 '18 at 09:14
  • Don't use a `java.util.Timer` in a Swing application but rather a `javax.swing.Timer` or [Swing Timer](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) instead. – Hovercraft Full Of Eels Jul 04 '18 at 10:44

0 Answers0