Make a special delay in gui java

I made this code in java, I want to delay after this part of the code, but when I use the delay or sleep codes, all the code sleeps for the delay time, but I want to see that this leads to changes in gui and then make a delay !! ! so what should i do? (I use swing gui)

for (int i = 0; i < 8; i++) {
    for (int j = 0; j < 8; j++) {
        if (matrisBazi[i][j] == 0) {
            jb[i][j].setBackground(Color.white);
        }
        if (matrisBazi[i][j] == 1) {
            jb[i][j].setBackground(Color.red);
        }
        if (matrisBazi[i][j] == 2) {
            jb[i][j].setBackground(Color.blue);
        }
    }
}
jb[i][j].addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent event) {   
    click(s);
    }
});

my actionPerformed void is called before and inside the click (); functions and makes this process how to add a swing timer code to this void ?! I think the timer does not work until it clicks (); finished.

+3
source share
2 answers

GUI Swing ( ), Thread.sleep(...), Swing . Swing.

, , for, actionPerformed Timer.

:

for (int i = 0; i < MAX; i++) {
  // do some animation using i
  Thread.sleep(sleepTime);
}

:

new Timer(sleepTime, new ActionListener(){
  private int i = 0;

  public void actionPerformed(ActionEvent evt) {
    if (i >= MAX) {
      // stop the animation
    }
    // do some animation with i
    i++;
  }
}).start();
+4

Swing (javax.swing.Timer), GUI.
: Swing

+4

All Articles