Progress Bar in Java Swing

In this section, you can learn how to handle progress bar in java swing.

Progress Bar in Java Swing


In this section, you can learn how to handle progress bar in java swing. This section shows you how the progress bar starts and stops with the timer. Through the given example you can understand how the progress bar is created for showing your work is in progress.

This program shows you a frame in which a button labeled by the string "Start", a progress bar and another is the label which has been used to display some messages. When you click on the "Start" button progress bar is started to progress the completed process in percent and the label which holds the text "" is change to with the label text "Downloading is in process......" in green color and the button is disabled. When the value of the progress bar is become 100% then the label text of the label is changed with the text "Downloading completed." in red color and "Start" button is enabled.

The label display the information about the Downloading process whether completed or not. But here, nothing is downloading through the program. This message on the label has been used only for showing in output of the program. For completion the process, there are some methods and APIs are used in the program has been explained as follows:

This is the class which creates the progress bar using it's constructor JProgressBar() to show the status of your process completion. The constructor JProgressBar() takes two argument as parameter in which, first is the initial value of the progress bar which is shown in the starting and another argument is the counter value by which the value of the progress bar is incremented. Here, the value of the progress bar is incremented by 20.

This is the method of the JProgressBar class which shows the complete process in percent on the progress bar. It takes a boolean value as a parameter. If you pass the true then the value will be seen on the progress bar otherwise not seen.

This is the method of the JProgressBar class which sets the value to the progress bar.

This the constructor of the Timer class which starts the timer for timing. This constructor takes two argument as parameter first is the interval (in milliseconds) of the timer and second one is the listener object. Time is started using the start() method of the Timer class.

Here is the code of the program:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.html.*;

public class SwingProgressBar{
  final static int interval = 1000;
  int i;
  JLabel label;
  JProgressBar pb;
  Timer timer;
  JButton button;

  public SwingProgressBar() {
  JFrame frame = new JFrame("Swing Progress Bar");
  button = new JButton("Start");
  button.addActionListener(new ButtonListener());

  pb = new JProgressBar(020);

  label = new JLabel("");
  JPanel panel = new JPanel();

  JPanel panel1 = new JPanel();
  panel1.setLayout(new BorderLayout());
  panel1.add(panel, BorderLayout.NORTH);
  panel1.add(label, BorderLayout.CENTER);

  //Create a timer.
  timer = new Timer(interval, new ActionListener() {
  public void actionPerformed(ActionEvent evt) {
  if (i == 20){
  String str = "<html>" "<font color=\"#FF0000\">" "<b>" 
"Downloading completed." "</b>" "</font>" "</html>";
  i = i + 1;

  class ButtonListener implements ActionListener {
  public void actionPerformed(ActionEvent ae) {
  i = 0;
  String str = "<html>" "<font color=\"#008000\">" "<b>" 
"Downloading is in process......." "</b>" "</font>" "</html>";
  public static void main(String[] args) {
  SwingProgressBar spb = new SwingProgressBar();

Download this example.



We have 1000s of tutorials on our website. Search Tutorials tutorials on our website.

Share on Google+Share on Google+

Progress Bar in Java Swing

Posted on: April 14, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Progress Bar in Java Swing   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
Mohammed Hazrin
April 23, 2011

It's great...
VipinJeet Tripathi
November 17, 2011
Progress Bar

It's quite good.... All the problems have been solved....
December 4, 2011
JProgressBar arguments

According to you the second argument in JProgressBar constructor determines the value of incrementation but even when i changed its value in coding in your program there was no change in performance of progress bar. How was it so?
Daniel Angelino
January 4, 2012

how to synchronize a task with a slash of progress in java
Imen Teyeb
April 25, 2012

I have a java project in eclipse and I need to know how to add a panel to pedestrians