repaint() Method in Java

stk1234 picture stk1234 · Jun 1, 2012 · Viewed 34.3k times · Source

I am playing around with the Java graphics class a little bit and I was just wondering--when is it necessary to call the method repaint()? I tried commenting it out and it didn't seem to affect my output GUI. I've seen it used a lot in Java GUI code that I've read, though. Would anyone mind explaining when to use it and when not to use it?

Answer

Tharwen picture Tharwen · Jun 1, 2012

It's never really necessary in most swing applications, because they handle it automatically (for common operations such as changing text values on buttons and adding data to a list box).

Generally, it's only if you've made some sort of change that swing won't automatically pick up - for example, you're not using a layout manager and are resizing components manually (because normally the layout manager repaints its components when necessary).