ProgressBar

A progress bar is used to indicate the progress of some task. In the example below, a timer is being used to simulate the task.

ProgressBar Example (Run Applet)

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

public class ProgressBarDemo extends JApplet
{
    @Override
    public void init()
    {
        this.setContentPane(new View());
    }

    public class View extends JPanel implements ActionListener
    {
        private final JButton runProgressBar = new JButton("Run Progress Bar");
        private final MyProgressBar progressBar = new MyProgressBar(runProgressBar, 500, 900);  // min and max values

        public View()
        {
            super();

            this.setLayout(new BorderLayout());
            this.add("North", this.progressBar);
            this.add("South", this.runProgressBar);

            this.runProgressBar.addActionListener(this);
        }

        @Override
        public void actionPerformed(ActionEvent e)
        {
            if (this.runProgressBar.isEnabled())
            {
                this.progressBar.start();
            }
        }

        public class MyProgressBar extends JProgressBar implements ActionListener
        {
            private final Timer timer = new Timer(10, this);   // set the speed of the timer
            private JButton progressBarButton;

            public MyProgressBar(JButton progressBarButton, int min, int max)
            {
                super(min, max);
                this.progressBarButton = progressBarButton;

                 // display the % progress on the progressBar
                this.setStringPainted(true);
            }

            public void start()
            {
                this.timer.start();
                this.progressBarButton.setEnabled(false);
            }

            public void stop()
            {
                this.timer.stop();
                this.progressBarButton.setEnabled(true);
            }

            public void reset()
            {
                this.stop();
                this.setValue(this.getMinimum());
            }

            @Override
            public void actionPerformed(ActionEvent ae)
            {
                // This method is called by the timer
                if (this.getValue() < this.getMaximum())
                {
                    this.setValue(this.getValue() + 1);
                }
                else
                {
                    this.reset();
                }
            }
        }
    }
}

Indeterminate ProgressBar

An Indeterminate progress bar does not progress from start to finish. Instead, it displays an animations to indicate that some work is being done.

An indeterminate progess bar automatically generate the animation. Therefore, in the example below, we do not need to use a timer to simulate a task that is being done.

ProgressBar_Indeterminate_Demo Example (Run Applet)

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

public class ProgressBar_Indeterminate_Demo extends JApplet
{
    @Override
    public void init()
    {
        this.setContentPane(new View());
    }

    public class View extends JPanel implements ActionListener
    {
        private final JButton runProgressBar = new JButton("Run Progress Bar");
        private final MyProgressBar progressBar = new MyProgressBar(runProgressBar, 500, 900);  // min and max values

        public View()
        {
            super();

            this.setLayout(new BorderLayout());
            this.add("North", this.progressBar);
            this.add("South", this.runProgressBar);

            this.runProgressBar.addActionListener(this);
        }

        @Override
        public void actionPerformed(ActionEvent e)
        {
            if (this.runProgressBar.isEnabled())
            {
                this.progressBar.start();
            }
        }

        public class MyProgressBar extends JProgressBar implements ActionListener
        {
            private JButton progressBarButton;

            public MyProgressBar(JButton progressBarButton, int min, int max)
            {
                super(min, max);
                this.progressBarButton = progressBarButton;

                // set displaying of message on the progress bar
                this.setStringPainted(true);
                this.setString("Not yet started");
            }

            public void start()
            {
                // display the message "running...." on the progressBar
                this.setString("running....");

                // set the progress bar's state to indeterminate
                this.setIndeterminate(true);

                this.progressBarButton.setEnabled(false);
            }

            public void stop()
            {
                this.progressBarButton.setEnabled(true);
            }

            public void reset()
            {
                this.stop();
                this.setValue(this.getMinimum());
            }

            @Override
            public void actionPerformed(ActionEvent ae)
            {
                // This method is called by the timer
                if (this.getValue() < this.getMaximum())
                {
                    this.setValue(this.getValue() + 1);
                }
                else
                {
                    this.reset();
                }
            }
        }
    }
}
 
<div align="center"><a href="../versionC/index.html" title="DKIT Lecture notes homepage for Derek O&#39; Reilly, Dundalk Institute of Technology (DKIT), Dundalk, County Louth, Ireland. Copyright Derek O&#39; Reilly, DKIT." target="_parent" style='font-size:0;color:white;background-color:white'>&nbsp;</a></div>