Progress Bar in Java

Progress Bar is a component used to show the status of the application that how much time is left in order to complete the task of a specified application.

Progress Bar in Java

Progress Bar in Java

     

Progress Bar is a component used to show the status of the application that how much time is left in order to complete the task of a specified application. In Java Progress Bar is created using swing. The Swing Class used for the implementation of Progress Bar is JProgressBar Class

Code Description 

In this Tutorial we want to describe you a program code that helps you in the implementation of Progress Bar in Java. This Program provides you how a Progress Bar show the status of your running application in progress.

The Program shows you a Class name' Progress Bar' with a button labelled with a name ' go'.

1)set String Painted- This is a Method that  will provides you the complete progress in percentage of the Progress Bar. This method takes Boolean as parameter. The value set to be true to see the status on progress bar, otherwise the status of progress bar would not be visible.

2)setValue( )- This Method provides you to set the value of the Progress Bar.

3)iterate( )-  This Method provides you if variable name  num is less than  1000 millisecond in while loop then it set the current thread to execute in Progress Bar, otherwise it would execute  the try block that remain thread to call sleep ( )method and remain inactive for 500 milliseconds, that is subsequently handled by catch block.

 

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

  public class ProgressBar extends JFrame
  {
  JLabel l1;
  JProgressBar current;
   JTextArea ta;
   JButton bu;
   Thread runner;
   int num = 0;

  public ProgressBar() 
{
   super("ProgressBar");

   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   JPanel pane = new JPanel();
   bu = new JButton("go");
   pane.setLayout(new GridLayout());
   current = new JProgressBar(0, 100);
   current.setValue(0);
   current.setStringPainted(true);
   pane.add(current);
   setContentPane(pane);
   pane.add(bu);
}


   public void iterate()
{
   while (num < 1000) 
{
   current.setValue(num);
   try 
{
   Thread.sleep(500);
 }
   catch (InterruptedException e)
 {


 }
  num += 90;
 }
 }

  public static void main(String[] arguments) {
  ProgressBar frame = new ProgressBar();
  frame.pack();
  frame.setVisible(true);
  frame.iterate( );
}

}

Output on Command Prompt


C:\saurabh>javac ProgressBar.java

C:\saurabh>java ProgressBar

 

On Execution of the Program code ,The Progress Bar display as follows