Swing Components / Progress Bar
Subtitles of the Movie
Some tasks take a while to complete and you need to let the user know that something is happening. Ideally you can also tell the user how long it's going to take. One common way of doing this is displaying a progress bar. Users are really quite patient and will wait for a task to finish as long they don't think the program has crashed. A program that demonstrates the operation of a progress bar must have a task running in a separate thread. It's this separate thread that advances the progress bar. The degree of completion is denoted by a number. Now, you can have the progress bar advance from any number to any other number, but I have always found it clearest to progress from zero to a hundred. You will need to import the JProgressBar component, which can be displayed in your window the same as any other component. On the progress bar, you specify the low and high numbers. This progress bar will go from zero to one hundred. The set value method is used to specify the amount of completion, so it's set to the beginning, to zero. This call causes the current value, the degree of completion to be painted in the middle of the progress bar. A new task is constructed and started. Notice that the progress bar is passed to the task. It's the job of the task to adjust the completion value as it progresses. This is the class that runs as a task. It implements the runable interface so it can be run as a separate thread. This run method is the executable loop of thread. It doesn't do much. As long as the Boolean running is true, it sleeps for two-tenths of a second, then it updates the completion number in the progress bar. Once the completion number has reached a hundred, the task stops running. Here's what it looks like. You can see the completion value being displayed on the progress bar. Actually you can run the numbers from any number to any other number and the displayed value would be from zero to a hundred percent. When it reaches the end, the tasks stops running, but the progress bar is still displayed. Now, you may be in a situation where you don't know what degree of completion you've got, but you can still use the progress bar. You can use it in indeterminate mode. Now, this form doesn't depend on any information from a task. All you do is display the progress bar. The construction is the same as it was before. You specify a range, but it doesn't matter because you won't be using it. You call this method to set the mode of operation of the progress bar to indeterminate. Then you just display it. It looks like this. It'll run back and forth this way as long as you keep it on display. This has one drawback however. It will continue to look exactly the same, even if your background thread has crashed. If that ever happens, your user won't trust your program to run again. It is normally better to display a changing completion value, even if it is an approximation. Here is an example of using the indeterminate mode and displaying a value at the same time. Now, this is exactly the same as the first example except for one thing. This method call right here is set to indeterminate mode. It looks like this. You can see that the value displays just fine and in indeterminate mode. You can have the value jump around, even run backwards and it will give the user the assurance that something is going on, even if there's no way to tell how close it is to being finished. If you prefer, you can have the progress monitor pop up as a dialog. The first argument on the constructor is the parent window, the same as with any dialog. The second argument is a string to be displayed in the window. The third is an optional string, the note. I'll have more about that here in a minute. After that, you specify the range of values for the progress bar, like we did before. And you set the starting value to whatever you want. You pass the monitor to the task the same as you did earlier with the progress bar and you just start setting values in it and it displays itself. If you want running numbers, you will have to specify the string you want displayed with a call to set note. Also the dialog has a cancel button which can be used to terminate the task. Here's how it looks when it runs. The monitor shows the values because they are changing. You can cancel a task at any time or wait until it's finished. Either way, the dialog closes. Coming up in the next lesson are sliders and spinner; two ways the user can change values using the mouse.
Tutorial Information
| Course: | Java 6 |
| Author: | Arthur Griffith |
| SKU: | 33858 |
| ISBN: | 1-934743-59-3 |
| Release Date: | 2008-02-29 |
| Duration: | 7 hrs / 92 lessons |
| Work Files: |
Yes |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 81,350 Video Tutorials (20,800 free)
- Video Available as Flash or QuickTime
- Over 782 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 