0

I am trying to update the value of the same label from Running to Paused after 5 seconds. The entire code is inside a timer with a delay of 1 min.

Timer timer= new Timer(60000, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            
            label.setText("Running...");

            //selenium code which takes around 5 seconds to run     
    
            label.setText("Paused");            
        }   

    });

However, the first setText is not working. The value is set to Paused after 5 seconds only. I tried using 2 labels, however both the values are being set after 5 seconds. Nothing is happening at the start.

How do I set the text as Running when the execution has started and then Paused after 5 seconds, until the timer hits the 1 minute mark?

  • You are calling `Thread.sleep` on the Swing EDT, the "event dispatch thread", and so you are not *pausing* your GUI with that call; you are ***freezing*** it, making it completely non-responsive, including all painting, while frozen. Get rid of that call to Thread.sleep. If you need to pause the GUI, that is what the Swing Timer is for. – Hovercraft Full Of Eels Aug 15 '21 at 17:58
  • So use a Timer with a delay of 5000 (5 seconds), and in its ActionListener set the text of the JLabel. You can tell the Timer to run only once by calling `.setRepeats(false)` on it. – Hovercraft Full Of Eels Aug 15 '21 at 18:00
  • That `Thread.sleep` is for understanding purpose. Actually, the code inside the actionPerformed is a selenium code which should run with 1 minute delay as set in Timer. But, even in that case, the first setText is not working. After the selenium code is complete, the value is set to **Paused**. – soujatya rakshit Aug 15 '21 at 18:03
  • You may think that the `Thread.sleep` is for "understanding purpose", but, again, it is totally messing up your Swing event thread. Read [Lesson: Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) to see why. Again, ***get rid of it***. Try to understand things *without* it – Hovercraft Full Of Eels Aug 15 '21 at 18:05
  • 1
    *"Actually, the code inside the actionPerformed is a selenium code which should run with 1 minute delay as set in Timer."* -- then this should be called within its own background thread, such as within a SwingWorker's `doInBackground()` method. The link that I provided above, will explain all. – Hovercraft Full Of Eels Aug 15 '21 at 18:06
  • Any long-running process needs to be run *off* of the EDT, else it blocks the EDT. Again, run the longer-running code, the Selenium code, for instance, in your SwingWorker. – Hovercraft Full Of Eels Aug 15 '21 at 18:07
  • Thanks! I'll have a look into the link. – soujatya rakshit Aug 15 '21 at 18:07
  • [For example](https://pastebin.com/ntW2CjHw) – Hovercraft Full Of Eels Aug 15 '21 at 18:27
  • Yes, I fixed it with the help of the link you provided. My code: [link](https://pastebin.com/fzNUwgrd) Thanks again for the guidance! – soujatya rakshit Aug 16 '21 at 08:26

0 Answers0