Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between JFrame.repaint() and JPanel.repaint()

Tags:

java

swing

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

like image 284
Ankit Avatar asked Jul 29 '12 10:07

Ankit


People also ask

What is the difference between paint () and repaint () methods?

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).

What does repaint () do in Java?

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().

What is difference between JFrame and frame?

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.

Should I use JFrame or JPanel?

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 ...


1 Answers

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.

like image 157
Hovercraft Full Of Eels Avatar answered Sep 21 '22 13:09

Hovercraft Full Of Eels