can please anyone explain the difference between JPanel.repaint()
method and JFrame.repaint()
method, i guess both calls the paintComponent()
method in JPanel.
Please clarify, thanks
The paint() method contains instructions for painting the specific component. The repaint() method, which can't be overridden, is more specific: it controls the update() to paint() process. You should call this method if you want a component to repaint itself or to change its look (but not the size).
repaint(): The repaint() is intended to allow various methods to call for a re-rendering of the component. No graphics context is needed for repaint().
JFrame is a top-level container that provides a window on the screen. A frame is actually a base window on which other components rely, namely the menu bar, panels, labels, text fields, buttons, etc. Almost every other Swing application starts with the JFrame window.
JPanel vs JFrame both are commonly used classes available in java; JPanel can be considered as a general container, which is used in case of complex or bigger functions which require grouping of different components together; on the other hand, JFrame is generally used to host simple elements used in a window like a ...
Calling repaint()
on any Component will add a request to the repaint manager to paint that component. If conditions are correct, the manager will then arrange to have the Component's paint(...)
method called. Since a Component's paint method will then call paintComponent(...)
, paintBorder(...)
and paintChildren(...)
this will have the component paint itself, its border and cascade painting recursively to all of its children, their children, their children's children, etc. Calling this on JFrame will cause this cascade to occur throughout the entire top-level window whereas calling it on a subcomponent will cause a repainting of that component and its children only.
Note that calling repaint()
does not guarantee that the repaint manager will paint the component, especially if there are stacked requests. For more on the details, please read Painting in AWT and Swing.
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