3

Im working in Java Swing using JWindows to display images... Ive made different JLabels contain the images that I want to display.. Im removing previous added components and adding new ones to be displayed on the same JWindow.. The problem is as follows..

The code works perfectly without the sleep function. I can display all the images on different windows or on same windows upon event completion.. However when I use sleep, nothing at all gets displayed during this period...

Is there any way to implement a delay for the images like a slideshow, and have the images painted before the delay?

            getContentPane().remove(startLabel);
            getContentPane().add(recordLabel1, "Center");
            setVisible(true);
            try {
                    Thread.sleep(500);
                } catch (InterruptedException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                getContentPane().remove(recordLabel1);
                getContentPane().add(recordLabel2, "Center");
            try {
                    Thread.sleep(500);
                } catch (InterruptedException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                getContentPane().remove(recordLabel2);
                getContentPane().add(recordLabel3, "Center");
            try {
                    Thread.sleep(500);
                } catch (InterruptedException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                getContentPane().remove(recordLabel3);
                getContentPane().add(recordLabel4, "Center");
            try {
                    Thread.sleep(500);
                } catch (InterruptedException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                getContentPane().remove(recordLabel4);
                getContentPane().add(pausedLabel, "Center");
                setVisible(false);  
Mat
  • 202,337
  • 40
  • 393
  • 406
  • Edit: I tried changing the image on a fixed JLabel rather than adding and removing components... Did not work! – Jeremy Craig Jul 27 '11 at 20:45
  • Edit: I also tried toggling many different overlapped windows containing the different images visible and invisible... Did not work! – Jeremy Craig Jul 27 '11 at 20:46
  • 2
    What the answers below don't explain is why nothing happens. It's because your code runs in the Swing event thread, and when you sleep, you pause that thread. The thread is needed to update and display Swing components though, so nothing will happen while it's asleep. – Bart van Heukelom Jul 27 '11 at 20:51
  • I think you should read http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html for a understanding how Swing's event-handling and threading basically works. – Roland Schneider Jul 27 '11 at 20:54

1 Answers1

8

This is a good time to use Swing Timers.

You should:

  • store your labels/pictures in an array rather than in different variables
  • set up a timer as indicated in the tutorial above
  • in the timer event, just rotate through your array of labels

All you need for that is an extra member in your class that stores what picture number you're currently displaying. When the timer fires, use that member to remove the current item from the pane, increase it (modulo the total number of elements you have), and insert the new one.

Mat
  • 202,337
  • 40
  • 393
  • 406
  • 1
    +1 for `javax.swing.Timer`; see also [`ImageLabelPanel`](http://stackoverflow.com/questions/3078178/jpanel-in-puzzle-game-not-updating/3078354#3078354). – trashgod Jul 27 '11 at 20:57