Swing is a single threaded framework. The Event Dispatching Thread is responsible for processing, amongst other things, repaint requests. Any action which stops the EDT from running, will prevent it from processing any repaints requests and other events, making your application look like it's hung...
You are also required to ensure that all updates to the UI are made from within the context of the EDT.
In your case, you are executing a loop and using Thread.sleep
, which are two big no, no's when dealing with Swing...
Take a look at Concurrency In Swing
As was mentioned in your duplicate question, you should be using a javax.swing.Timer
.
This means you are going to have to revamp your loop conditions to work, for example...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class ColorPane {
public static void main(String[] args) {
new ColorPane();
}
public ColorPane() {
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 Color[] colors = new Color[]{Color.RED, Color.GREEN, Color.BLUE};
private int colorIndex = -1;
public TestPane() {
Timer timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
colorIndex++;
if (colorIndex >= colors.length) {
colorIndex = 0;
}
setBackground(colors[colorIndex]);
}
});
timer.start();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}