0
int p = 0;
int z = 0;
while (p < plaintext.length) {

    while (z < 26) {
        buttons[plaintext[p]+z*26].setBackground(Color.GREEN);
        z++;
    }

    z = 0;
    p++;                        
}

I am setting a column of 26 buttons to have a green background with as starting point the variable P in a 26 by 26 grid of buttons. So my question is how can I have a delay between each column changing background color so that it first shows the first column turning green and waits a few seconds and then shows the second column turning green and waits a few seconds and so on.

thank you in advance

programmer
  • 35
  • 7
  • 1
    Possible duplicate of [How do I make a delay in Java?](https://stackoverflow.com/questions/24104313/how-do-i-make-a-delay-in-java) – Guy Oct 22 '18 at 12:00

2 Answers2

0

Use Thread.sleep(2000); to put the main thread on sleep. 2000are milliseconds that means 2 seconds delay

Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78
  • If I do that the delays will stack and after the delays all the columns of buttons will turn green at the same time. This is all done in an actionlistner within a class if that makes a difference. – programmer Oct 22 '18 at 12:25
  • you are always setting the green color right? that's what the code says – Danyal Sandeelo Oct 22 '18 at 12:26
  • Yeah that's right but what I am trying to achieve is to set a column of buttons green with the while loop with the variable z and then wait before doing it again with the increased p value. – programmer Oct 22 '18 at 12:29
0

With the help of the link provided by the user Guy I was able to change my code and get the result I wanted. Here is the code if anyone is wondering.

private static void visual() {

        while (z < 26) {
            buttons[plaintext[p]+z*26].setBackground(Color.GREEN);
            z++;
        }

        z = 0;
        if (p < plaintext.length) p++;

}

final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
        executorService.scheduleAtFixedRate(new Runnable() {
                 @Override
                 public void run() {
                         visual();
                 }
         }, 0, 1, TimeUnit.SECONDS);
programmer
  • 35
  • 7