File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes calling repaint in an ActionListener Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "calling repaint in an ActionListener" Watch "calling repaint in an ActionListener" New topic

calling repaint in an ActionListener

Karen Nelson
Ranch Hand

Joined: Aug 19, 2005
Posts: 57
I feel like I should know why this is happening, but...

I am trying to show a simple animation when the user clicks a button. So I have a loop in the ActionPerformed method which calls repaint and then sleeps. But what happens is that only the last repaint actually happens. The other calls to repaint never seem to occur (that is, my System.out.println's don't come out).

First, can anyone tell me why this happens?

And of course how to fix it?

Thank you!!! Karen.


Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

All painting and event handling happens on one thread, the "Swing event thread." Until your event handler returns, repaint() calls result in paint requests being queued but not executed. You can't run your animation loop in an event handler: you should kick off a new thread from the handler, and run the loop in that new thread. This can be very simple to do -- i.e.,

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: calling repaint in an ActionListener
jQuery in Action, 3rd edition