Nasif Imtiaz Ohi
Nasif Imtiaz Ohi

Reputation: 1713

changing images in jLabel after certain intervals

I had to make a sort of animation thing in netbeans gui. So I was studying about swing timer on the internet and from what i found i worte a method which will change images in jLabel after certain time periods.

public void animation() throws InterruptedException {
    ActionListener taskPerformer = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent evt) {
            //...Perform a task...
            t++;
            System.out.printf("Reading SMTP Info. %d\n",t);
            if(t%2==1){
                jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/oncsreen_keypad/a.jpg")));
            }
            else{
                jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/oncsreen_keypad/b.jpg")));
            }
        }
    };
    Timer timer = new Timer( 1000 , taskPerformer);
    //timer.setRepeats(false);
    timer.start();

    Thread.sleep(5000);
}

this method is called nowhere. But if the System.out.printf works then changing image in jLabel should also work. But actually in the run those lines are having no effect on the jLabel.

So what should be the right approach.

Upvotes: 1

Views: 5830

Answers (1)

Patrick
Patrick

Reputation: 4562

Dont stop the main thread with Thread.sleep.., use the Swing Timer and give him the delay, when your image should change.

I made a small example for you.

This is the class which generates the JFrame with a JPanel, which contains the JLabel.

package timerdemo;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

/**
 *
 * @author ottp
 * @version 1.0
 */
public class Gui extends JFrame {

    private JLabel jLabel;
    private Timer timer;
    private boolean chromeShown;

    public Gui() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(800, 600);
        JPanel panel = new JPanel(new BorderLayout());
        jLabel = new JLabel(new ImageIcon("/home/ottp/Downloads/chrome.png"));
        chromeShown = true;

        panel.add(jLabel);
        timer = new Timer(5000, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(chromeShown) {
                    jLabel.setIcon(new ImageIcon("/home/ottp/Downloads/ok.png"));
                    chromeShown = false;
                } else {
                    jLabel.setIcon(new ImageIcon("/home/ottp/Downloads/chrome.png"));
                    chromeShown = true;
                }
            }
        });
        timer.start();

        this.getContentPane().add(panel);
        this.setVisible(true);
    }
}

And start it...

package timerdemo;

import javax.swing.SwingUtilities;

/**
 *
 * @author ottp
 */
public class TimerDemo {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Gui();
            }
        });

    }
}

After starting the timer in your Gui class, the image on the JLabel will be changed every 5 seconds, condition for this is the boolean flag. You could use your if... else construct also there.

Hope this helps

Patrick

Upvotes: 2

Related Questions