title: How to create a delay in Swing
body: <p>I made a blackjack game, and I want the AI player to pause between taking cards. I tried simply using Thread.sleep(x), but that makes it freeze until the AI player is done taking all of his cards. I know that Swing is not thread safe, so I looked at Timers, but I could not understand how I could use one for this. Here is my current code:</p>
  
  <pre><code>while (JB.total &lt; 21) {
  
            try {
              Thread.sleep(1000);
            } catch (InterruptedException ex) {
              System.out.println("Oh noes!");
            }
  
            switch (getJBTable(JB.total, JB.aces &gt; 0)) {
              case 0:
                JB.hit();
                break;
              case 1:
                break done;
              case 2:
                JB.hit();
                JB.bet *= 2;
                break done;
            }
          }
  </code></pre>
  
  <p>BTW, the hit(); method updates the GUI.</p>