Working With ProgressBar in Swing


Introduction

In this article we describe a very popular or common feature of any softawre installation. I think all of you have seen progress bars. Now we are describing how we can make your own progress bars. A Progress Bar is a component used to show the status of the application; how much time is to complete the task of the application.
Sometimes a task running within a program might take a while to complete. A user-friendly program provides some indication to the user that the task is occurring, how long the task might take, and how much work has already been done. One way of indicating work, and perhaps the amount of progress, is to use an animated image.

For Creating progress bar you have to follow some steps

Step 1 :
Importing a necessary package.

import java.util.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

Step 2 : Defining the necessary variables.

JProgressBar   prbr;

JButton        b;

JLabel         l;

JPanel         P;

Step 3 : Create the components within the constructor (its optional), but it is not necessary to define them in the constructor; you can define then anywhere etc.

public MyProgressBar()

{

// creating a panel p

P = new JPanel();

// set the size of panel

P.setPreferredSize( new Dimension(400,400));
//add to thye conentpane

getContentPane().add(P);

// Create a label and progress bar

l = new JLabel("Waiting for your click...");

/ set the size of label

l.setPreferredSize( new Dimension(380,45));

//add to thye conent panel

P.add(l);

// create the progress bar

prbr = new JProgressBar();

// set the size of progress bar

prbr.setPreferredSize( new Dimension(300,20));

//set minimum value for starting the progress bar

prbr.setMinimum(0);

//set maximum value for end point of  the progress bar

prbr.setMaximum(1000);

// set the value at starting is 0

prbr.setValue(0);

// set bounds means location in panel

prbr.setBounds(30,50,360,30);

//now add in pannel

P.add(prbr);

//creating button with name 'go'

b = new JButton("Go");

P.add(b);

b.addActionListener(this);

setTitle("MY Progress Bar ready");

setSize(400,400);

setBackground(Color.Green);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

Step 4: Override the action performed method.

public void actionPerformed(ActionEvent event)

{

if(event.getSource() == b)

{

// Prevent more button presses

b.setEnabled(false);

// Perform all of our bogus tasks

for(int iCtr = 1; iCtr < 1001; iCtr++)

{

// Do some sort of simulated task

DoTask(iCtr);

// Update the progress indicator and label

l.setText("Performing task " + iCtr + " of 1000");

Rectangle labelRect = l.getBounds();

labelRect.x = 0;

labelRect.y = 0;

l.paintImmediately(labelRect);

prbr.setValue(iCtr);

Rectangle progressRect = prbr.getBounds();

progressRect.x = 0;

progressRect.y = 0;

prbr.paintImmediately(progressRect);

}

}

}

Step 5 : Create the Dotask method functionality.

public void DoTask(int iCtr)

{

Random random = new Random(iCtr);

// Waste some time

for(int iValue = 0; iValue < random.nextFloat() * 1000; iValue++)

{

System.out.println("Value=" + iValue);

}

}

Step 6 : Creatie the main method and create an object of this class within main.

public static void main(String args[])

{

// Create an instance of the test application

MyProgressBar mF    = new MyProgressBar();

mF.setVisible(true);

mF.pack();

}


Complete Code

import java.util.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

class MyProgressBar extends JFrame implements ActionListener

 {

JProgressBar    prbr;

JButton        b;

JLabel         l;

JPanel         P;

public MyProgressBar()

{

setTitle( "MY Progress Bar ready" );

setSize( 400, 400 );

setBackground( Color.green );

P = new JPanel();

P.setPreferredSize( new Dimension( 400, 400 ));

getContentPane().add(P);

// Create a label and progress bar

l = new JLabel( "Waiting to start tasks..." );

l.setPreferredSize( new Dimension(380,45));

P.add(l);

prbr = new JProgressBar();

prbr.setPreferredSize( new Dimension(300,20));

prbr.setMinimum(0);

prbr.setMaximum(1000);

prbr.setValue(0);

prbr.setBounds(30,50,360,30);

P.add(prbr);

b = new JButton("Go");

P.add( b);

b.addActionListener( this );

}

public void actionPerformed(ActionEvent event)

{

if(event.getSource()== b)

{

// Prevent more button presses

b.setEnabled(false);

// Perform all of our bogus tasks

for(int iCtr= 1;iCtr < 1001;iCtr++)

{

// Do some sort of simulated task

DoBogusTask(iCtr);

// Update the progress indicator and label

l.setText("Performing task " + iCtr + " of 1000");

Rectangle labelRect = l.getBounds();

labelRect.x = 0;

labelRect.y = 0;

l.paintImmediately(labelRect);

prbr.setValue(iCtr);

Rectangle progressRect = prbr.getBounds();

progressRect.x = 0;

progressRect.y = 0;

prbr.paintImmediately(progressRect);

}

}

}

public void DoBogusTask(int iCtr )

{

Random random = new Random(iCtr);

// Waste some time

for( int iValue = 0; iValue < random.nextFloat() * 1000; iValue++ )

{

System.out.println("iValue=" + iValue);

}

}

public static void main(String args[])

{

// Create an instance of the test application

MyProgressBar mF = new MyProgressBar();

mF.setVisible(true);

mF.pack();

}

}

Output

Cmd output:

Progressbarcmd.jpg

The initial output of the frame with progress bar.

Progressbar.jpg


After clicking on the go button:

Progressbar1.jpg

Resources

The Use of JTable in Swing

Working With JMenu in Swing
Describing the Java Packages
Progressbar control in Silverlight 3.0
Use of ByteStreams and CharacterStreams in JAVA

Up Next
    Ebook Download
    View all
    Learn
    View all