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 Repaint method not working from button call Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Repaint method not working from button call" Watch "Repaint method not working from button call" New topic

Repaint method not working from button call

Sam Nassiri

Joined: Jun 01, 2010
Posts: 9
Dear all,

I have constructed a button that when pressed calls a method to repaint a JFrame. The repaint is called from a method called refresh, which is placed inside a for loop. The intended result is that the JFrame is refreshed at the end of each loop, i.e. 5 times if x is equal to 5. Unfortunately the repaint only happens at the end of the for loop, and only one change is observed. Below is the code.

The repaint() method is contained within thisSession.refresh().

Many Thanks
Martin Vajsar

Joined: Aug 22, 2010
Posts: 3728

This behaviour is expected.

Components do not immediately update themselves when you call repaint(). Instead, they remember they need to be redrawn. Actual painting is carried by the Event Dispatch Thread when it is not busy. Since you do all your processing from within Event Dispatch Thread, it is busy all the time, so no redrawing occurs and the UI is updated only once when you finish all your work. (This is good, because this way you don't need to worry to repaint every component only once when processing actions. There are typically many repaint() calls in the guts of Swing libraries and in many applications as well; if every repaint() call triggered immediate redraw, the UI would flicker tremendously.)

What you're trying to do is probably somewhat similar to animations. Google for animation in swing, there are lots and lots of articles on the web about this topic.
Sam Nassiri

Joined: Jun 01, 2010
Posts: 9
Thanks Martin,
I have instead adapted the program so the user can watch day increments rather than a day-by-day display.
I agree. Here's the link:
subject: Repaint method not working from button call
It's not a secret anymore!