1

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.

George Brighton
  • 5,131
  • 9
  • 27
  • 36
Nasif Imtiaz Ohi
  • 1,563
  • 5
  • 24
  • 45
  • Which method is called nowhere? – vanza Feb 21 '14 at 19:21
  • 1
    Please edit your question to include an [*Minimal, Complete, Tested and Readable Example*](http://stackoverflow.com/help/mcve) that exhibits the problem you describe; some examples are cited [here](http://stackoverflow.com/a/14432646/230513). – trashgod Feb 21 '14 at 19:37
  • Please have a look at this [example](http://stackoverflow.com/a/10837751/1057230). Some additional [links](http://stackoverflow.com/a/9866659/1057230) that might can help :-) – nIcE cOw Feb 22 '14 at 03:16

1 Answers1

2

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

Patrick
  • 4,532
  • 2
  • 26
  • 32
  • 1
    As a Swing developer, the main thread we don't tend to care about, what we care about the Event Dispatching Thread. You should take a look at [Initial Threads](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html) – MadProgrammer Feb 21 '14 at 20:43
  • 1
    One way to get image(s) for an example (that works for everybody) is to hot-link to the images seen in [this answer](http://stackoverflow.com/a/19209651/418556). – Andrew Thompson Feb 22 '14 at 04:24