aspose file tools*
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 Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "calling repaint in an ActionListener" Watch "calling repaint in an ActionListener" New topic
Author

calling repaint in an ActionListener

Karen Nelson
Ranch Hand

Joined: Aug 19, 2005
Posts: 57
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

Joined: Jul 08, 2003
Posts: 24168
    
  30

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: http://aspose.com/file-tools
 
subject: calling repaint in an ActionListener
 
Similar Threads
TwoButtons App From "HeadFirst Java" Book
why is this not compiling
Simple Gui Question (Head First Java)
why does this code fail?
GUI Example (HFJ)