As Philippe Whitehouse states in his answer, you are blocking the Swing Event Dispatch Thread event flow with a call to Thread.sleep(...) .
Given that you already took the time to create an ActionListener , it would be easiest to use javax.swing.Timer to control text clearing. To do this, you can add a field to your GUI class:
private Timer clearTimer = new Timer(5000, this);
In the constructor for the GUI disable the snooze function, since you really only need one shot:
public GUI() { clearTimer.setRepeats(false); createFrame(); }
Then actionPerformed can be changed to use this to start the timer / clear the field:
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) {
source share