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

6

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

    
asked by anonymous 28.07.2017 / 16:18

1 answer

6

Briefly, you should use both.

repaint() alerts Swing that some area of the screen is inappropriate, "dirty". It is necessary to delete the image of old child components removed by removeAll() for example.

revalidate() recalculates the layout through the layout manager. Usually used when adding components on the panel. This causes the children components of the panel to "reposition" but not the panel itself. This method is called specific cases .

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

    
28.07.2017 / 16:54