Skip to main content

Swing / AWT

All Swing events happen on one thread (The EDT, or Event Dispatch Thread). Doing processing on this thread in your normal code logic will cause your GUI to freeze up while your processing is done, since all Swing rendering is waiting for that to finish.

Use the javax.swing.SwingUtilities class to solve this problem, by using its InvokeLater method:

SwingUtilities.invokeLater(() -> {
  someButton.setText("I am a button");
});

This adds whatever code you put inside the anonymous function to the end of the swing queue, and then continues with your code as normal. Use this to update the GUI without making it freeze.