Progress Bar
Summary:
The progress bar is a key UI element that is used primarily to provide feedback to the user. Traditionally, the user does not interact with the progress bar. The progress bar is generally displayed when the application is busy, and is an indication to the user that the application is performing a task, and not frozen. In addition, the progress bar displays a percentage of the task that is completed, in order to give the user a visual cue as to when the application will finish this task. The progress bar is usually featured in the forefront (while installing an application, for instance), but can also appear in a status bar or appear in a minor fashion that does not demand the user’s total attention. The progress bar appears in many applications and although styles vary greatly (including some derivatives that do not use a bar or progress amount), it is commonly understood by users and is important in keeping the user informed.
Where to Find It:
The API for the current JProgressBar can be found here.
The Basics:
Java's Swing API provides basic progress bar support in the form of the JProgressBar. Since it is a JComponent, it is fairly simple to implement given any prior experience with the Swing libraries. Even assuming no experience, it simply needs to be instantiated and added to something that can contain JComponents , such as a JPanel. Here is a simple implementation of a progress bar:
...
// create the new widget
JProgressBar progressBar = new JProgressBar();
// tell the progress bar to appear to be doing something (with no fixed length or percentage completed)
progressBar.setIndeterminate(true);
// add the progress bar to some panel to make it appear somewhere
panel.add(progressBar);
...
Perhaps you'd like the progress bar to show some sort of percentage of the task as done, in that case, you would continue by:
...
// set the maximum size of the bar (an integer representing the maximum states it can have)
// if it was a mail program, returning mail, this would be the number of messages it is currently receiving, such as 7.
progressBar.setMaximum(maxLengthOfBar);
// set the current value of the bar (an integer that is less than or equal to the maximum)
// this should be the amount of the task that has completed, like for a mail program, 5 out of 7 messages, this would be 5.
progressBar.setValue(currentLength);
// this tells the progress bar to stop showing the indeterminate state.
progressBar.setIndeterminate(false);
...
Customization:
The element has a few customization tricks that you may want to use. For instance, the bar can be horizontal or vertical, depending on its orientation property. This can be set in the constructor, or in the following method call:
...
// Set the progress bar to vertical orientation. progressBar.setOrientation(JProgressBar.VERTICAL);
...
Another interesting property is the StringPainted property. This describes whether or not a string representing the progress will be drawn inside the progress bar. To set this property, call the following method:
...
// Tells the progress bar to draw the progress string
progressBar.setStringPainted(true);
...
By default, Java will determine this string. (Such as 21%). If you wish to change it to your own value though, call this:
...
// Set a custom string to draw inside the progress bar
progressBar.setString("Halfway Done");
...
Best Practices:
It is important to use progress bar's when the user might otherwise consider the application frozen. For the most part, users want to know what is happening in the application and a progress bar will let them feel in control. Applications that do not let the user know what is going on are not very comforting. A key consideration, however, should be whether or not the progress bar will pop up in its own screen or appear in a status bar or in the background. Try to stay away from limiting what the user can do, and only put a progress bar in some sort of new window that can't be closed if it is absolutely necessary.
Limitations:
The progress bar that comes with Java's major limitation is customization of how it really looks. It may not be as visually pleasing as something that is hand-written or comes from a third party. However, consider the application and the amount of time you are willing to spend, as the JProgressBar can be implemented very quickly.
Things to Watch Out for When Implementing a Progress Bar:
Most importantly, make sure to update your progress bar! There is no gain to having a progress bar that doesn't show progress. It may be necessary to write some sort of observer or other construct to ensure that the progress bar is properly updated as progress takes place.
References And Resources: