Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

calling repaint in an ActionListener

 
Karen Nelson
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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.



and

 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.,

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic