Typically, one of the possibilities is to use a custom RepaintManager. The following is a simplified and very non-optimized example:
public class FreezableRepaintManager extends RepaintManager { final Set<Component> frozen = new HashSet<Component>(); public void freeze(Container c) { frozen.add(c); for (Component child : c.getComponents()) { if (child instanceof Container) { freeze((Container) child); } else { frozen.add(child); } } } public void thaw(final Container c) { frozen.remove(c); for (Component child : c.getComponents()) { if (child instanceof Container) { thaw((Container) child); } else { frozen.remove(child); } } c.repaint(); } @Override public void addDirtyRegion(JComponent c, int x, int y, int w, int h) { if (!frozen.contains(c)) { super.addDirtyRegion(c, x, y, w, h); } } }
Install a redraw manager somewhere at the beginning of your code using RepaintManager.setCurrentManager() , and then use freeze(componentTree) before looking at a series of operations and follow thaw(componentTree) after completion.
This works for most components, but unfortunately, JScrollPane is one for which it is not enough, since it makes a more complex picture than most. Therefore, you may need a JScrollPane whose createViewport () returns a view port that can suppress glare, for example:
class FreezableViewport extends JViewport { private boolean frozen; public void freeze() { frozen = true; } public void thaw() { frozen = false; } @Override protected boolean computeBlit(int dx, int dy, Point blitFrom, Point blitTo, Dimension blitSize, Rectangle blitPaint) { if (frozen) { return false; } return super.computeBlit(dx, dy, blitFrom, blitTo, blitSize, blitPaint); } }
It will also require freezing before a series of modifications and with the aforementioned redraw manager, thawing before the repaint dispatch method is called.