ISCL is a Intelligent Information Consulting System. Based on our knowledgebase, using AI tools such as CHATGPT, Customers could customize the information according to their needs, So as to achieve

How to Make a Progress Bar

1


Sometimes applications have to perform a task that takes a reasonable amount of time to complete. An application with a helpful GUI will provide an indication to the user of how long the task will take to perform. This indication typically is given using a progress bar. The JProgressBar class creates a graphical component for providing this kind of feedback.

How Much Progress?


The JProgressBar shows a horizontal bar that changes color from grey to blue as a particular task is being performed.

Sometimes it's easy to determine the progress of a task and hence how long it's going to take to finish. Other times it's less clear and the time taken to complete a task might be indeterminate. The JProgressBar distinguishes a determined task length by showing the percentage of progress on the horizontal bar,

The Example Program GUI


A complete Java code listing for this article can be found in A Progress Bar Example Program. It displays a simple GUI which has a JButton, JProgressBar and two JCheckBoxes. The JButton starts a simulation of a task being performed and as it progresses the JProgressBar is told how far the task has progressed and displays the information graphically. The two JCheckBoxes control whether the JProgressBar is showing progress for a determined or indeterminate task.

The task is simulated using the SwingWorker class - it performs the 'task' in a separate thread and provides feedback to the JProgressBar. There is a doInBackground method which simulates a task by performing a count from 0 to 100, pausing the thread for a short time after each step up.

There is also a done method which is called when the SwingWorker thread is finished.

Creating a Progress Bar


When creating a progress bar you set a minimum and maximum value. These values could be anything that fits the task the program is providing feedback for. To create a progress bar with a minimum of 0 and a maximum of 100:

progressBar = new JProgressBar(0, 100);

Determined or Indeterminate?


To switch between the two progress modes of the JProgressBar is achieved by calling the setStringPainted method:

progressBar.setStringPainted(true);
When this method is true a String showing the percentage of the progress is displayed. If it's false then no string is displayed, just the blue bar. As a task progresses it might become the case that the remaining time of a previously indeterminate task is now known. By calling and setting the setStringPainted method to true you can switch the JProgressBar to start showing that percentage.

Or, the String can be changed from showing n% to a custom String by using the setString method:

progressBar.setString("Getting there..");

Showing the Progress


As the task the JProgressBar is tracking continues the progress of the progress bar is set by using the setValue method, For example, to set the progress bar at 50%:

progressBar.setValue(50);
In the example code in A Progress Bar Example Program the setValue method is called as the simulated task counts from 0 to 100. This provides the JProgressBar with the information it needs to display the progress.

As indeterminate tasks are tricky to show progress it's best to just show that something is happening. Continue to use the setValue method to show progress going upwards and if by the time the progress has reached the maximum value of the JProgressBar set the value by to the minimum and start the progress tracking all over again. This will display a progress bar that keeps on being filled and give the user a sense that something is still going on.

Source...
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.