Writing Your Own Animation Loop with javax.swing.Timer

| No TrackBacks
javax-swing-timer-demo-screencap.jpgIn the last week or so, while working on some Java Swing code for a project at work, I hit Swing bug #4480705.  When the user clicked a button to start an "activation", I changed the ImageIcon on a JButton to an animated GIF to indicate activity (e.g., "we're doing something, please wait").  So, while the application was busy, I disabled the JButton and set its ImageIcon to an animated spinner.  Loading this animated GIF and setting it on the JButton using setIcon() caused the Java Swing EventQueue threads to deadlock, as described here, and my entire application hung.  Well technically speaking, not the entire application, but just the Swing portion of the JVM.  Clearly, Swing doesn't like animated GIF's as much as the Sun documentation claims.

The solution to this deadlock is to avoid using animated GIF's all together, and write your own animate loop using javax.swing.Timer.  That's exactly what I did, and it works great.

For your viewing pleasure, I wrote up a quick demo/example (see Beavis and Butthead screencap) which demonstrates the use of javax.swing.Timer and how you can integrate it into your Swing application.

Download just the source, or the full Eclipse project.

Rock on.

Did You Find this Helpful?

Did you find this post helpful, or at least, interesting?

  

Send Mark a Direct Message

If you'd like to send me a direct message, please do so below. However, I do not publicly post comments or messages submitted directly to me. So, if you're going to try to SPAM me, or my blog, you're pretty much wasting your time.

400 characters remaining

Error

About Mark

A Silicon Valley native, Mark Kolich is a full-time Software Engineer, a casual entrepreneur, and a consultant for hire. A web technologies expert, his current focus is on building powerful and robust cloud-driven web-applications using Java, PHP, Perl, AJAX, DHTML, CSS, and JavaScript. His favorite programming languages are PHP, Java and JavaScript. He uses Linux, enjoys biking to work, loves building great software, and always writes elegant, readable, and maintainable code.

No TrackBacks

No trackbacks attached to this entry.

Twitter (@markkolich)

Translate

About this Entry

This page contains a single entry by Mark Kolich published on December 20, 2009 12:55 PM.

UAC Prompt From Java: CreateProcess error=740, The requested operation requires elevation (ShellExecuteEx Runas Example) was the previous entry in this blog.

A Better Way to Include JavaScript and CSS in your Web-Apps (Break Proxy and Browser Caching too!) is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.