Reputation:
Code:
try {
ImageIcon ico2=new ImageIcon("src/on1.png");
jLabel42.setIcon(ico2);
Thread.sleep(100);
ImageIcon ico3=new ImageIcon("src/on2.png");
jLabel42.setIcon(ico3);
Thread.sleep(100);
ImageIcon ico4=new ImageIcon("src/on3.png");
jLabel42.setIcon(ico4);
Thread.sleep(100);
ImageIcon ico5=new ImageIcon("src/on4.png");
jLabel42.setIcon(ico5);
Thread.sleep(100);
ImageIcon ico7=new ImageIcon("src/on5.png");
jLabel42.setIcon(ico7);
Thread.sleep(100);
ImageIcon ico8=new ImageIcon("src/on6.png");
jLabel42.setIcon(ico8);
Thread.sleep(100);
ImageIcon ico9=new ImageIcon("src/on7.png");
jLabel42.setIcon(ico9);
Thread.sleep(100);
ImageIcon ico10=new ImageIcon("src/on8.png");
jLabel42.setIcon(ico10);
Thread.sleep(100);
ImageIcon ico6=new ImageIcon("src/on1.png");
jLabel42.setIcon(ico6);
OffOn1=1;
} catch (InterruptedException ex) {
}
This code works same as :
Thread.sleep(900);
ImageIcon ico6=new
ImageIcon("src/on1.png");
jLabel42.setIcon(ico6);
OffOn1=1;
And my question is WHY?!!!
Upvotes: 0
Views: 44
Reputation: 691735
Here we go again.
The thread that is supposed to paint the label is the one executing these lines of code: the event dispatch thread. Since it's busy executing these lines of code, it can't paint anything until it has finished executing them.
You should use a separate thread to sleep and regularly change the icon of the label. Make sure though, that every swing component is always accessed from the EDT. The easiest way to do that is to use a Swing Timer.
Read the tutorial on swing and concurrency.
Upvotes: 1