title: Java Swing: Change Text after delay
body: <p>Basically, I have this game where once guesses the correct answer it starts a new game with a new word. I want to display <code>Correct!</code> but after three seconds, change it to a empty string. How do I do that?</p>
  
  <p><strong>My attempt:</strong></p>
  
  <pre><code>if (anagram.isCorrect(userInput.getText()))
      {
  
          anagram = new Anagram();
          answer.setText("CORRECT!");
          word.setText(anagram.getRandomScrambledWord());
          this.repaint();
          try
          {
          Thread.currentThread().sleep(3000);
          }
          catch (Exception e)
          {
          }
          answer.setText("");
  
      } else
      {
          answer.setForeground(Color.pink);
          answer.setText("INCORRECT!");
      }
  </code></pre>
  
  <p><strong>Edit:</strong></p>
  
  <p><strong>My solution:</strong></p>
  
  <pre><code>private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
      {
          // TODO add your handling code here:
      if (anagram.isCorrect(userInput.getText()))
      {
          answer.setText("CORRECT!");
  
          ActionListener taskPerformer = new ActionListener()
      {
      public void actionPerformed(ActionEvent evt)
      {
          anagram = new Anagram();
          word.setText(anagram.getRandomScrambledWord());
          answer.setText("");
          userInput.setText("");
      }
      };
      Timer timer = new Timer(3000, taskPerformer);
      timer.setRepeats(false);
      timer.start();
      } else
      {
          answer.setForeground(Color.pink);
          answer.setText("INCORRECT!");
      }
      }
  </code></pre>
  
  <p>I am not sure, but I hope that I am following MadProgrammer's advice and not blocking the event itself, but the new thread. I will look up Java Timer also.</p>