title: Swing - Thread.sleep() stop JTextField.setText() working
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/14074329/using-sleep-for-a-single-thread">using sleep() for a single thread</a>  </p>
  </blockquote>
  
  
  
  <p>I'm having issues with JTextField.setText() when using Thread.sleep(). This is for a basic calculator I'm making. When the input in the input field is not of the correct format I want "INPUT ERROR" to appear in the output field for 5 seconds and then for it to be cleared. The setText() method <em>did</em> work when I just set the text once to "INPUT ERROR" and by printing out the text in between I found it does work with both that and the setText("") one after the other. The problem arises when I put the Thread.sleep() between them.
  Here's a SSCCE version of the code:</p>
  
  <pre><code>import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.util.regex.Pattern;
  import javax.swing.*;
  
  public class Calc {
      static Calc calc = new Calc();
  
      public static void main(String args[]) {
          GUI gui = calc.new GUI();
      }
  
      public class GUI implements ActionListener {
  
          private JButton equals;
  
          private JTextField inputField, outputField;
  
          public GUI() {
              createFrame();
          }
  
          public void createFrame() {
              JFrame baseFrame = new JFrame("Calculator");
              baseFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              JPanel contentPane = new JPanel();
              BoxLayout layout = new BoxLayout(contentPane, BoxLayout.Y_AXIS);
              contentPane.setLayout(layout);
              baseFrame.setContentPane(contentPane);
              baseFrame.setSize(320, 100);
  
              equals = new JButton("=");
              equals.addActionListener(this);
  
              inputField = new JTextField(16);
              inputField.setHorizontalAlignment(JTextField.TRAILING);
              outputField = new JTextField(16);
              outputField.setHorizontalAlignment(JTextField.TRAILING);
              outputField.setEditable(false);
  
              contentPane.add(inputField);
              contentPane.add(outputField);
              contentPane.add(equals);
  
              contentPane.getRootPane().setDefaultButton(equals);
              baseFrame.setResizable(false);
              baseFrame.setLocation(100, 100);
  
              baseFrame.setVisible(true);
          }
  
          /**
           * When an action event takes place, the source is identified and the
           * appropriate action is taken.
           */
  
          @Override
          public void actionPerformed(ActionEvent e) {
              if (e.getSource() == equals) {
                  inputField.setText(inputField.getText().replaceAll("\\s", ""));
                  String text = inputField.getText();
                  System.out.println(text);
                  Pattern equationPattern = Pattern.compile("[\\d(][\\d-+*/()]+[)\\d]");
                  boolean match = equationPattern.matcher(text).matches();
                  System.out.println(match);
                  if (match) {
                      // Another class calculates
                  } else {
                      try {
                          outputField.setText("INPUT ERROR"); // This doesn't appear
                          Thread.sleep(5000);
                          outputField.setText("");
                      } catch (InterruptedException e1) {
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>I'm not actually using a nested class but I wanted it to be able to be contained in one class for you. Sorry about how the GUI looks but again this was to cut down the code. The the important section (<code>if (e.getSource() == equals)</code>) remains unchanged from my code. The simplest way to give an incorrect input is to use letters.</p>