What is the difference between revalidate() and repaint()?

Asked

Viewed 570 times

6

When working with swing, we usually call one of these methods after some change in screen components. But after all, what’s the difference between using repaint() or revalidate()? In what situation should I use one or the other, or even both together?

1 answer

6


Briefly you should use both.

The repaint() warns Swing that some area of the screen is inadequate, "dirty". It is necessary to erase the image of old child components removed by removeAll() for example.

The revalidate() recalculates the layout through the layout manager. Usually used when adding components in the panel. This makes the components children the panel "reposition" but not the panel itself. This method is called in specific cases.

Remembering that revalidate() is the call of the method invalidate() on all components of the container, which marks them as invalid, and shortly after the validate(). This calls the method layoutComponents() layout manager. If a layout change is required, the repaint() is called.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.