0

Having a weird problem. I'm trying to have a series of events:
1) gui event happens
2) 3 second pause
3) another gui event happens.

However, for some reason, the first gui event happens after the delay, dispite clearly being stated before the delay! Another interesting factor here is that non-gui elements in the same location are executed in the expected order (see the code below).

                System.out.println("before delay");  //line executed before delay             
                image.setVisible(false);             //line executed AFTER delay
                repaint();

                try{
                    TimeUnit.SECONDS.sleep(3);
                }
                catch(InterruptedException error)
                {
                }

                image.setVisible(true);              //executed after delay
                System.out.println("after delay");   //executed after delay
Blaine
  • 332
  • 1
  • 4
  • 18
  • 1
    Possible duplicate of [how do I refresh a GUI in Java?](http://stackoverflow.com/questions/12865803/how-do-i-refresh-a-gui-in-java) – Azodious Dec 07 '16 at 08:10
  • That question appears to ask about something completely different. I am talking specifically about the sleep function, and how it appears to affect commands before it. Using repaint(); does not solve anything – Blaine Dec 07 '16 at 08:20
  • have you tried calling `repaint()` after `setVisible(false)`? – Azodious Dec 07 '16 at 08:22
  • I have. It didn't change anything – Blaine Dec 07 '16 at 08:23
  • the sleep works fine, but you don't refresh the gui once the image becomes invisible, therefore it stays visible. give us more code we can work with – XtremeBaumer Dec 07 '16 at 08:24
  • @XtremeBaumer the code's a bit confusing right now. I'll try to clean it up and update you with a more concise version. In the meantime, can you think of any way to refresh the gui? I have tried putting repaint(), revalidate(), and doLayout() after setVisible(false), but nothing has worked. – Blaine Dec 07 '16 at 08:29
  • @Blaine Can you add call to `repaint()` also in question. – Azodious Dec 07 '16 at 08:30
  • where do you execute the code you posted? – XtremeBaumer Dec 07 '16 at 09:00
  • Sorry guys. A lot of work just piled up, and I'll be super busy for a time here. I'll try to update the full code here on Saturday – Blaine Dec 07 '16 at 10:29
  • Alright, well, I can't say I have an "answer" because my solution doesn't really solve the gui refreshing. However, I have solved the problem by using a different code: int delay = 8000; // time to delay in milliseconds ActionListener taskPerformer = new ActionListener() { public void actionPerformed(ActionEvent evt) { //code to be run after delay } }; – Blaine Dec 09 '16 at 14:45

0 Answers0