This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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: 24183

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
Similar Threads
Simple Gui Question (Head First Java)
GUI Example (HFJ)
why does this code fail?
TwoButtons App From "HeadFirst Java" Book
why is this not compiling