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

may someone like to explain the difference between the JPanel.repaint() method and JFrame.repaint() , I think both calls to the paintComponent() method in JPanel.

Please clarify, thanks

+8
java swing
source share
3 answers

Calling repaint() on any Component will add a request to the repaint dispatcher to draw this component. If the conditions are correct, the manager then organizes a call to the Component paint(...) method. Since the Paint Component method will call paintComponent(...) , paintBorder(...) and paintChildren(...) , this will have the component paint, its border and cascading picture recursively for all of its children, their children, their children - children etc. Calling this on a JFrame will cause this cascade throughout the top-level window, while calling it on a subcomponent will cause the component to be redrawn and its children.

Note that calling repaint() does not guarantee that the redraw manager will draw the component, especially if there are complex requests. For more details, see Painting in AWT and Swing .

+18
source share

1. When the repaint() method is repaint() , then the Event handler thread notifies all the components, and then each component calls their paintComponent() method .

2. Calling JPanel.repaint() will have the entire component in JPanel for repaint() , and when calling JFrame.repaint() will be the whole component inside JFrame - repaint() .

3. Call JFrame.repaint () to redraw JPanel, too, without the need for JPanel.repaint () ....

+9
source share

In short

  • JPanel.repaint() will repaint itself and its children.
  • JFrame.repaint() repaint itself and its children. But since JFrame is the base, it will recolor every component inside.

Check the "@Hovercraft Full Of Eels" answer for details.

+2
source share

All Articles