0

I am trying to display different images in 3 different jlables. There is a folder with images in them. I have display the images in the jlable and the image has to change after 60 seconds. No matter what I try I cannot do it. Can somebody help me.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366

1 Answers1

2

The simple answer is, use a Swing Timer, it allows you to schedule a callback in the future, which is delivered within the context of the Event Dispatching Thread, making it safe to update the UI from, for example...

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JLabel label;
        private File[] images;
        private int imageIndex;

        public TestPane() {
            setLayout(new BorderLayout());
            label = new JLabel();
            label.setHorizontalAlignment(JLabel.CENTER);
            label.setVerticalAlignment(JLabel.CENTER);
            add(label);
            images = new File("...").listFiles();
            imageIndex = -1;
            nextImage();
            Timer timer = new Timer(1000, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    nextImage();
                }
            });
            timer.start();
        }

        protected void nextImage() {
            if (images.length > 0) {
                imageIndex++;
                if (imageIndex >= images.length) {
                    imageIndex = 0;
                }
                try {
                    BufferedImage image = ImageIO.read(images[imageIndex]);
                    label.setIcon(new ImageIcon(image));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }

    }

}

Have a look at How to use Swing Timers for more details.

If your images are large or are coming from a slow service (like over the internet), you might consider using a SwingWorker instead, this allows you to perform long running or blocking operations in the background (off the EDT), but which are easier to synchronise updates back to the EDT with. Have a look at Refresh Image in JLabel with Timer for more details

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366