Painting and Repainting

Our last topic in this chapter deals with repainting in Swing. Repainting is a somewhat arcane topic that most people don’t really feel the need to talk about. This section is orchestrated to give you a better feel for how painting and repainting via the repaint manager and JComponent works. You typically do not need to get involved with the RepaintManager class, and only the extremely brave will override it. However, there are some instances where a firm understanding of the repaint manager can avoid confusion.

Swing Responsibilities

Recall that Swing uses lightweight components, which are drawn inside heavyweight top-level containers. Hence, the operating system will not deal with the repainting of those components. To continue the analogy first presented in the lightweight and heavyweight discussion earlier, Swing is responsible for painting and repainting everything inside of its own easels. Consequently, Swing delegates this duty to a RepaintManager class to organize and schedule repainting when told to do so.

The RepaintManager Class

The RepaintManager class is responsible for painting and repainting parts of components that have become dirty, or regions that need to be repainted. Note that the “dirty region” does not necessarily include the entire region of the component, but often only a portion of it. The RepaintManager is also charged with the second responsibility of revalidating components that have been marked invalid. Both responsibilities ultimately ...

Get Java Swing now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.