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().
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.
Joined: Jun 01, 2010
I have instead adapted the program so the user can watch day increments rather than a day-by-day display.
subject: Repaint method not working from button call