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: |
Scroll to post title



























Recent Comments
Js Kit Comments Correct Usage Of The Permalink And Path Attributes
http://store.taobao.com/shop/view_shop.htm?asker=wangwang&shop_nick=a333b444
http://item.taobao.com/auction/item_detail-0db2-40a79e949a57400b6b96edd149670677.htm
http://item.taobao.com/auction/item_detail-0db2-1d66cafb681edb96c634b894d2b1df3f.htm
http://item.taobao.com/auction/item_detail-0db2-a37ac99a003784d0c9b38150416d4c58.htm
http://item.taobao.com/auction/item_detail-0db2-f84bf08e8c573335b7d7ffe73a9d611b.htm
http://item.taobao.com/auction/item_detail-0db2-61c03a65b354488484ea0ec3076785b2.htm
http://item.taobao.com/auction/item_detail-0db2-e61b6c352c15689ee7eeca843f5345da.htm
http://item.taobao.com/auction/item_detail-0db2-958a432e957ab5c9e9b4fd6326a6ff55.htm
http://item.taobao.com/auction/item_detail-0db2-1d66cafb681edb96c634b894d2b1df3f.htm
http://item.taobao.com/auction/item_detail-0db2-484419d0d030152d16dd9dd2039c94c2.htm
http://item.taobao.com/auction/item_detail-0db2-43831f4a2dc611bccd1024ca3cf2b5f9.htm
http://item.taobao.com/auction/item_detail.htm?item_num_id=4955248190
http://item.taobao.com/auction/item_detail.htm?item_num_id=4957300818
http://item.taobao.com/auction/item_detail.htm?item_num_id=4957109628
Tue, 11 May 2010 15:02:41 +0000
Js Kit Comments Correct Usage Of The Permalink And Path Attributes
http://a333b444.taobao.com
Tue, 11 May 2010 14:55:51 +0000
Js Kit Comments Correct Usage Of The Permalink And Path Attributes
GOOD
Tue, 11 May 2010 14:46:49 +0000
Installing Jdownloader In Ubuntu
Very nice tutorial, works like charm, ty very much
cheers!
Fri, 02 Apr 2010 10:58:11 +0000
Js Kit Comments Correct Usage Of The Permalink And Path Attributes
http:www.qq8080.com.cn
Fri, 02 Apr 2010 06:49:22 +0000
Js Kit Comments Correct Usage Of The Permalink And Path Attributes
http://www.ioiojewelry.com
Mon, 29 Mar 2010 04:02:13 +0000