This example is compiled with JDK 1.1.8 so that it is cross-browser/platform independent. The goal is to monitor the progress of a Thread.
Just a quick history:
First, to be platform independent, and my goal was to be able to run this on Mac OS9, I needed
to throw out all Swing components and deal with a pure AWT implementation. Finding a progress bar canvas was very easy. There are plenty of them out there. The one that I found that is really nice, because it comes close to the Swing model (although it would be nicer if the methods were the same) is
at geocities. With a few tweaks, it could look the same. However, the example only demonstrated how the progress bar works based on ActionListeners that were hooked up to the scroll bars.
Next, I was really impressed with this example, but it started getting a little hairy because it requires the calling Applet to implement Runnable. The main thread being monitored sits within the run() method of the applet. I wanted to move the main thread outside of the Applet.
Finally, after scouring the web, I finally found my answer on my bookshelf. I re-read Horstmann's Core Java(TM) 2, Volume II. In the first chapter on Threads, there is a great example called TimerTest.java. The example clearly shows how you need to setup a daemon thread to poll on the Thread in order to update the progress bar.
But how to monitor a running thread? Flipping to the Advanced Swing chapter, there is another example that shows you the swing version: ProgressBarTest.java. So how to translate into AWT? It's not that hard. You can download the source code here: ProgressBar.jar. The example below has a timer that is polling a Thread that is just counting to 100. I had to put a Thread.sleep(10) in the polling timeout method in order to give you the animated effect.
Just click start to run
L&M Art Gallery.com
See how we brought a small business to the Internet with a full service solution.
Go to the site
©2004 SM Consulting International LLC