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?
The repaint()
refreshes the view (component), so whenever you make any change on the component, you must call it. For instance, if you rotate the graphical component, you must make a call to repaint()
in order to see the change on the containing component
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With