Skip to content

Testing Two Types of ProgressBar in Java

September 9, 2013

Objective:

–> 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

JAVA CODE

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

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);
        determinate.setValue(0);
        determinate.setStringPainted(true);

        // 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;
                timer.start();
                indeterminate.setIndeterminate(true);
            }
        });

        // 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(Color.black, 5, false));
        myPanel.add(myButton, BorderLayout.NORTH);
        myPanel.add(determinate, BorderLayout.CENTER);
        myPanel.add(indeterminate, BorderLayout.SOUTH);

        // myPanel added to myFrame
        myFrame.setContentPane(myPanel);

        // 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
                    timer.stop();
                    determinate.setValue(0);
                    // after the process obove finishes, a new thread will run
                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            // stops the indeterminate obj
                            indeterminate.setIndeterminate(false);
                        }
                    });
                }
                // this will increase the progressbar
                i += 1;
                determinate.setValue(i);

            }
        });

        // set and pack all the necessary settings and components
        myFrame.pack();
        myFrame.setVisible(true);
        myFrame.setDefaultCloseOperation(myFrame.EXIT_ON_CLOSE);
    }    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

DOWNLOAD COMPLETE CODES HERE

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.

Advertisements
Leave a Comment

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

TurtleAndRobot.com

Children's Book Reviews

Malav Shah

official Web Site

My Road to Programming

The journey of how I taught myself to program.

paritoshmishra

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.

ProgramThat;

A simple guide to programming...

ITechonology

Information Techonology

Rahul's Work

My Work Tips and Tutorial on Various Tech.

Turnip Farmers

Kingdom Scans

Gigaom

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

The Neighborhood

telling the story from every vantage point

HarsH ReaLiTy

A Good Blog is Hard to Find

VentureBeat

News About Tech, Money and Innovation

retireediary

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: