Working With ProgressBar in Swing

Introduction

 
In this article, we describe a very popular or common feature of any software 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.
 

Creating progress bar in Java

 
Step 1: Importing a necessary package.
  1. import java.util.*;  
  2. import java.awt.*;  
  3. import java.awt.event.*;  
  4. import javax.swing.*;  
  5. import javax.swing.event.*;  
Step 2: Defining the necessary variables.
  1. JProgressBar prbr;  
  2. JButton b;  
  3. JLabel l;  
  4. 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.
  1. public MyProgressBar()  
  2. {  
  3.     // creating a panel p  
  4.     P = new JPanel();  
  5.     // set the size of panel  
  6.     P.setPreferredSize(new Dimension(400400));  
  7.     //add to thye conentpane  
  8.     getContentPane().add(P);  
  9.     // Create a label and progress bar  
  10.     l = new JLabel("Waiting for your click...");  
  11.     // set the size of label  
  12.     l.setPreferredSize(new Dimension(38045));  
  13.     //add to thye conent panel  
  14.     P.add(l);  
  15.     // create the progress bar  
  16.     prbr = new JProgressBar();  
  17.     // set the size of progress bar  
  18.     prbr.setPreferredSize(new Dimension(30020));  
  19.     //set minimum value for starting the progress bar  
  20.     prbr.setMinimum(0);  
  21.     //set maximum value for end point of  the progress bar  
  22.     prbr.setMaximum(1000);  
  23.     // set the value at starting is 0  
  24.     prbr.setValue(0);  
  25.     // set bounds means location in panel  
  26.     prbr.setBounds(305036030);  
  27.     //now add in pannel  
  28.     P.add(prbr);  
  29.     //creating button with name 'go'  
  30.     b = new JButton("Go");  
  31.     P.add(b);  
  32.     b.addActionListener(this);  
  33.     setTitle("MY Progress Bar ready");  
  34.     setSize(400400);  
  35.     setBackground(Color.Green);  
  36.     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  37. }  
Step 4: Override the action performed method.
  1. public void actionPerformed(ActionEvent event)  
  2. {  
  3.     if (event.getSource() == b)  
  4.     {  
  5.         // Prevent more button presses  
  6.         b.setEnabled(false);  
  7.         // Perform all of our bogus tasks  
  8.         for (int iCtr = 1; iCtr < 1001; iCtr++)  
  9.         {  
  10.             // Do some sort of simulated task  
  11.             DoTask(iCtr);  
  12.             // Update the progress indicator and label  
  13.             l.setText("Performing task " + iCtr + " of 1000");  
  14.             Rectangle labelRect = l.getBounds();  
  15.             labelRect.x = 0;  
  16.             labelRect.y = 0;  
  17.             l.paintImmediately(labelRect);  
  18.             prbr.setValue(iCtr);  
  19.             Rectangle progressRect = prbr.getBounds();  
  20.             progressRect.x = 0;  
  21.             progressRect.y = 0;  
  22.             prbr.paintImmediately(progressRect);  
  23.         }  
  24.     }  
  25. }  
Step 5: Create the Dotask method functionality.
  1. public void DoTask(int iCtr)  
  2. {  
  3.     Random random = new Random(iCtr);  
  4.     // Waste some time  
  5.     for (int iValue = 0; iValue < random.nextFloat() * 1000; iValue++)  
  6.     {  
  7.         System.out.println("Value=" + iValue);  
  8.     }  
  9. }  
Step 6: Create the main method and create an object of this class within main.
  1. public static void main(String args[])  
  2. {  
  3.     // Create an instance of the test application  
  4.     MyProgressBar mF = new MyProgressBar();  
  5.     mF.setVisible(true);  
  6.     mF.pack();  
  7. }  
Complete Code
  1. import java.util.*;  
  2. import java.awt.*;  
  3. import java.awt.event.*;  
  4. import javax.swing.*;  
  5. import javax.swing.event.*;  
  6. class MyProgressBar extends JFrame implements ActionListener  
  7. {  
  8.     JProgressBar prbr;  
  9.     JButton b;  
  10.     JLabel l;  
  11.     JPanel P;  
  12.     public MyProgressBar()  
  13.     {  
  14.         setTitle("MY Progress Bar ready");  
  15.         setSize(400400);  
  16.         setBackground(Color.green);  
  17.         P = new JPanel();  
  18.         P.setPreferredSize(new Dimension(400400));  
  19.         getContentPane().add(P);  
  20.         // Create a label and progress bar  
  21.         l = new JLabel("Waiting to start tasks...");  
  22.         l.setPreferredSize(new Dimension(38045));  
  23.         P.add(l);  
  24.         prbr = new JProgressBar();  
  25.         prbr.setPreferredSize(new Dimension(30020));  
  26.         prbr.setMinimum(0);  
  27.         prbr.setMaximum(1000);  
  28.         prbr.setValue(0);  
  29.         prbr.setBounds(305036030);  
  30.         P.add(prbr);  
  31.         b = new JButton("Go");  
  32.         P.add(b);  
  33.         b.addActionListener(this);  
  34.     }  
  35.     public void actionPerformed(ActionEvent event)  
  36.     {  
  37.         if (event.getSource() == b)  
  38.         {  
  39.             // Prevent more button presses  
  40.             b.setEnabled(false);  
  41.             // Perform all of our bogus tasks  
  42.             for (int iCtr = 1; iCtr < 1001; iCtr++)  
  43.             {  
  44.                 // Do some sort of simulated task  
  45.                 DoBogusTask(iCtr);  
  46.                 // Update the progress indicator and label  
  47.                 l.setText("Performing task " + iCtr + " of 1000");  
  48.                 Rectangle labelRect = l.getBounds();  
  49.                 labelRect.x = 0;  
  50.                 labelRect.y = 0;  
  51.                 l.paintImmediately(labelRect);  
  52.                 prbr.setValue(iCtr);  
  53.                 Rectangle progressRect = prbr.getBounds();  
  54.                 progressRect.x = 0;  
  55.                 progressRect.y = 0;  
  56.                 prbr.paintImmediately(progressRect);  
  57.             }  
  58.         }  
  59.     }  
  60.     public void DoBogusTask(int iCtr)  
  61.     {  
  62.         Random random = new Random(iCtr);  
  63.         // Waste some time  
  64.         for (int iValue = 0; iValue < random.nextFloat() * 1000; iValue++)  
  65.         {  
  66.             System.out.println("iValue=" + iValue);  
  67.         }  
  68.     }  
  69.     public static void main(String args[])  
  70.     {  
  71.         // Create an instance of the test application  
  72.         MyProgressBar mF = new MyProgressBar();  
  73.         mF.setVisible(true);  
  74.         mF.pack();  
  75.     }  
  76. }  
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


Similar Articles