Java: repaint() Waking Nightmare


This post was originally published in 2009
It may contain stale & outdated information. Or it may have grown more awesome with age, like the author.

You know those times, while coding, when it feels like you’ve drifted into a horrible nightmare where nothing you do works?

This happened to me today.

I wanted the little app I’m making to display a green tick or a red cross depending on whether the user had entered valid credentials. Simple? Yeah. Everything went really well, I extended a JPanel to handle the tick/cross display area (tickCross), got the POST code going, strung together the rest of the GUI elements.

The problem was that tickCross.repaint() wasn’t reliably repainting. about 30% of the time the image wouldn’t be painted. The method was being called, but nothing was happening. I tried Google, re-read the related Sun Java docs and was reminded that: “repaint() does not actually paint. It calls the peer repaint which enqueues a request in some platform-dependent way inside the native GUI for a repaint.” – MindProd. Great. My OS was deciding when I was allowed to draw.

Somewhat more irritated, I then had to resort to every programmer’s backup tool: trial and error.

Thanks Rob (we make websites), for laughing at this post and emailing me the following:


?! How about:


I swear I tried that, honest!

After much frustration, I struck a solution. It’s not elegant, I’d even say it’s hacksih, but it works.


Cross or tick when I damn well want them, every time.

No comments | Trackback