This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Don't use a Long when you need a long.
Why are you adding components in that listener? You don't want to do that. It gives a very peculiar effect if components are added after the GUI has been displayed. I suggest you consider card layout which allows components to disappear to the back or appear towards the front.
You should consider a Timer instead of Thread.sleep.
Don't write empty catch blocks, otherwise you would never know whether an Exception has occurred.
The reason you don't see anything for four seconds and then see the second image is that the thread that's responsible for drawing those images is the same thread that handles events: the Event Dispatch Thread (EDT).
What happens is that when the ActionEvent is triggered, the EDT adds the label with the first image, sleeps for two seconds, adds the second image, and sleeps for two more seconds. Only after that's done is it then able to repaint the screen, at which point you'll finally see the second image.
As Campbell indicated, you need to look into using Timer to avoid this.
Joined: Apr 30, 2014
Thank you for your response.
All the examples I could find on the web using Timer seemed quite complex.
Can you send a code fragment.
I did try the following but the results were the same.