Waiting for Swing's elements repaint completion
I want to be able to wait for Swing's repaint
completion.
Example:开发者_StackOverflow
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?
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.
You can override paintComponent()
:
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
...
// repaint finished here
}
精彩评论