I have a Vector of 4 Frames that consist of a 2D array of JButtons that is contained within a JPanel. I am trying to add these to a JFrame so that they will display an animation when looped.
I have an add method that works when I increment it manually, however when I put it in a for loop the frame stays blank as if it has not been repainted. The terminal prints the correct index numbers.
public void addNewFrame() throws InterruptedException {
for (int i = 0; i < 10; i++) {
if (index == 3) {
System.out.println(index);
remove(model.getFrame(3));
revalidate();
repaint();
index = 0;
add(model.getFrame(index), BorderLayout.CENTER);
revalidate();
repaint();
Thread.sleep(300);
} else {
System.out.println(index);
remove(model.getFrame(index));
revalidate();
repaint();
index++;
add(model.getFrame(index), BorderLayout.CENTER);
revalidate();
repaint();
Thread.sleep(300);
}
}
Could this be a concurrency issue? Should I be adding the 2D array of buttons to the JPannel which is running in it's own thread?