pbp
pbp

Reputation: 1481

Waiting for Swing's elements repaint completion

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?

Upvotes: 3

Views: 2767

Answers (2)

camickr
camickr

Reputation: 324108

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.

Upvotes: 3

trashgod
trashgod

Reputation: 205785

You can override paintComponent():

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    ...
    // repaint finished here
}

Upvotes: 1

Related Questions