3

I want to be able to wait for Swing's repaint completion.

Example:

frame.repaint();
wait_for_repaint_to_finish();
//work

I have something like this:

SwingUtilities.invokeAndWait(new Runnable() {

    public void run() {
        try {
            frame.repaint();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});

Is this the correct way to do it?

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
pbp
  • 1,461
  • 17
  • 28

2 Answers2

3

The question is why do you need something like this? Why would you ever repaint the entire frame and wait for it to be complete. If we know what you are attempting to do we can probably give a better suggestion.

repaint() just schedules painting to be done. The RepaintManager will potentially consolidate multiple paint requests and do them at one time to make paintng more efficient.

Having said that, if you really need to force a repaint you can use

JComponent.paintImmediately(...);

But this should only be used as a last resort and not to fix a design problem.

camickr
  • 321,443
  • 19
  • 166
  • 288
1

You can override paintComponent():

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    ...
    // repaint finished here
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045