Similar problem here but didn't find an answer: Why is paintComponent() continuously and asynchronously being called without explicit repaint() call?

I have a JPanel that I am drawing onto

class DrawPanel extends JPanel {

final void paintComponent(Graphics g) {
        super.paintComponent(g);
      // some graphics drawing stuff
} }

and then adding this to a JScrollPane. However if I put a system.out.println() in the paintComponent method I can see its continuously being called. Any way to stop this? According to the link its possible due to the jpanel being covered

有帮助吗?

解决方案

Generally paintComponent() does not paint continually. It get invoked occasionally when Swing determines it needs be painted.

If your method is being invoked continually then I can think of a couple of possible problems. You are:

  1. manually invoking repaint()
  2. changing a property of the component in the paintComponent() method which then automatically invokes repaint()

其他提示

The paintComponent calls come from Swing's Event Dispatch Thread. It gets called everytime the component needs to be repainted. If you resize the component or bring it back from minimized state, then it's repainted. Of course if you cover it with another component then the repainting will be called less. The other component will have a paintComponent method too though. Nothing to worry about.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top