When I change GUI (add/remove components) in an actionPerformed method, how can I force it update immediately? Calling revalidate seems delay the update until method actionPerformed returns.

Scott Stanchfield

The actionPerformed method is being called by the AWT thread.

The AWT thread is the thread that actually performs the repaint/revalidate request. Because that thread is tied up processing actionPerformed, it cannot do the update.

The only way you can do this (assuming you're doing some processing that takes a bit of time) is to start another thread for the processing. Note that if this other thread wants to update the GUI components, it should use Swing's invokeLater functionality to schedule the GUI update with the AWT thread. For example:

public void actionPerformed(ActionEvent e) {
  new Thread() {
    public void run() {
      // update GUI
      SwingUtilities.invokeLater(new Runnable() {
        public void run() {

(Sorry about the nested anon inner classes, but hopefully you'll get the point.)

Starting a thread to do a long-running action inside the actionPerformed method frees the AWT thread to actually update the GUI when you want it to