0

I am trying to blink the background color of a JButton in Java Swing. This is the Code I came up with:

if (result) {
        ((JButton) e.getSource()).setBackground(Color.green);
    } else {
        ((JButton) e.getSource()).setBackground(Color.red);
    }
    System.out.println("done");
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e1) {
        e1.printStackTrace();
    }
    ((JButton) e.getSource()).setBackground(new Color(44, 51, 60));

However, the Code just skips the first .setBackground and just does the delay thing. When I remove the second .setBackground, I can see the first one gets actually executed but there is a wait time until the Program continues running.

I already tried using Timeunits and frame.repaint(), no different result. What am I missing?

Gian K
  • 56
  • 2

0 Answers0