Question

If I create a custom JPanel (where all of the game I'm making will be displayed on the screen), and override it's paintComponent() for that purpose. Is everything inside that method going to be run on the EDT? And what about other methods inside that same class?

What if I call repaint() on the custom JPanel from inside the run() method of a thread I made? Will paintComponent() still be run on the EDT?

Was it helpful?

Solution

Yes, everything inside that method is going to be run on the EDT including any methods that you invoke within paintComponent().

And what about other methods inside that same class?

If they are called from paintComponent() or from actionPerformed(), etc, then they will also be on the EDT!

What if I call repaint() on the custom JPanel from inside the run() method of a thread I made? Will paintComponent() still be run on the EDT?

repaint() on a non-EDT thread will schedule a repaint -> paintComponent() to be run on the EDT. So yes, still on the EDT! Btw, you can test this by placing a break-point in your paintComponent method and seeing which thread is suspended.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top