Java: repaint() Waking Nightmare
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:
tickCross.repaint(); tickCross.setVisible(false); tickCross.setVisible(true);
?! How about:
Invalidate();
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.
1 2 3 | tickCross.repaint(); tickCross.setVisible(false); tickCross.setVisible(true); |
Cross or tick when I damn well want them, every time.
Like this post? Move it on along with:
Email |
delicious |
Digg |
Tweet |
Reddit |
Newsvine |
Furl |
Google |
Stumble |
HaoHao
| Trackback: |
No related posts.





























Recent Comments
Installing Jdownloader In Ubuntu
Thank You!!!!
It works like a charm!!!!!!!!!!!!
Tue, 05 Jan 2010 13:02:35 +0000
Arrived In Shanghai
Done, look left!
Sun, 03 Jan 2010 15:37:03 +0000
Arrived In Shanghai
Hi :)
I'll see what I can do!
Sun, 03 Jan 2010 14:23:07 +0000
Arrived In Shanghai
Hi Mike. It would be very useful to have the time date and weather conditions in Shanghai, on your site. Be seeing you soon. Love NZMum.
Sat, 02 Jan 2010 23:30:41 +0000
Js Kit Comments Correct Usage Of The Permalink And Path Attributes
I think it could be better
Thu, 31 Dec 2009 09:01:44 +0000