Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 563
pbp Avatar asked Mar 06 '26 05:03

pbp


1 Answers

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.

like image 117
camickr Avatar answered Mar 07 '26 19:03

camickr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!