Waiting for Swing Elements to Change

I want to wait for the completion of Swing repaint.

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 being done right?

+3
source share
2 answers

The question is, why do you need something like this? Why do you redraw the entire frame and wait for it to complete. If we know what you are trying to do, we can probably give a better offer.

repaint () just plans to draw. RepaintManager will potentially consolidate several paint requests and do them all at once to make paintng more efficient.

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

JComponent.paintImmediately(...);

, .

+2

paintComponent():

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

Source: https://habr.com/ru/post/1795049/


All Articles