Skip to content

Testing Two Types of ProgressBar in Java

September 9, 2013


–> when button is press, starts the progressbar: the determinate and indeterminate.

–> when loading is at 100%, the two progressbar stops the progress.

–> know the difference of determinate and indeterminate progressbar.

–> get to know how to use it on an actual program.

NOTE:  you can use >shift + middle mouse button< to scroll in the code from side to side


// imports the necessary packages
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class AllProgressBarTest {
    // the objects

    JFrame myFrame;
    JButton myButton;
    JProgressBar determinate;
    int i;
    Timer timer;

    // constructor where we initializes
    public AllProgressBarTest() {

        // set appropriate settings to our progressbar
        determinate = new JProgressBar(0, 10);

        // another type of progressbar
        final JProgressBar indeterminate = new JProgressBar();

        // the button with listener to start the timer obj and the indeterminate obj.
        myButton = new JButton("press");
        myButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                i = 0;

        // sets the title
        myFrame = new JFrame("test my progress bar");

        // wrap and positions, add the obj to the myPanel
        JPanel myPanel = new JPanel();
        myPanel.setLayout(new BorderLayout());
        myPanel.setBorder(BorderFactory.createLineBorder(, 5, false));
        myPanel.add(myButton, BorderLayout.NORTH);
        myPanel.add(determinate, BorderLayout.CENTER);
        myPanel.add(indeterminate, BorderLayout.SOUTH);

        // myPanel added to myFrame

        // the timer obj that will loop every 1 sec
        timer = new Timer(1000, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // if i is equal to 20, the progress is already 100% so we stop
                if (i == 20) {
                    // set the value to zero and stop the timer obj
                    // after the process obove finishes, a new thread will run
                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            // stops the indeterminate obj
                // this will increase the progressbar
                i += 1;


        // set and pack all the necessary settings and components
    }    public static void main(String[] args) {
        Runnable runnable = new Runnable() {
            public void run() {
                AllProgressBarTest aPBT = new AllProgressBarTest();
        // this will start the our home made object
        new Thread(runnable).start();

Simple, Free Image and File Hosting at MediaFire

Snapshot of the progressbar when the button is clicked. progress at 20%

Simple, Free Image and File Hosting at MediaFire

Snapshot after the loading finishes. pressing the button again would restart the progressbar


DETERMINATE PROGRESS BAR: is where w can determine the status of the progress, showing 20%, 66%, etc.

INDETERMINATE PROGRESS BAR: is where we cannot, only this bar bouncing from side to side until the process is finished.

If you’ve got anything to add/opinions/corrections, just comment.

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Children's Book Reviews

Malav Shah

official Web Site

My Road to Programming

The journey of how I taught myself to program.


Look at what's brewing up guys! we all are work in progress. So don't meander, lets micromesh everything and have candid conversation about every problem related to C, C++ and JAVA. We can rejuvenate the crinkled faces.


A simple guide to programming...


Information Techonology

Rahul's Work

My Work Tips and Tutorial on Various Tech.

Turnip Farmers

Kingdom Scans


Technology news, trends and analysis covering mobile, big data, cloud, science, energy and media

The Neighborhood

is under renovation in preparation for our 6th Season


News About Tech, Money and Innovation


The Diary of a Retiree

50 Year Project

My challenge to visit 192 countries, read 1,001 books, and watch the top 100 movies

[BTW] : Ben Trube, Writer

Writer, Programmer, Singer

My Arch Walkthrough

Arch linux made easy

Don Charisma

because anything is possible with Charisma

SutoCom Solutions

Success & Satisfaction with the Cloud

%d bloggers like this: