Home Java Java-get-example Progress Bar in Java
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Progress Bar in Java

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

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 1, 2008

Ask Questions?    Discuss: Progress Bar in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
justin
March 31, 2011
Progress Bar in Java

how to make "num" have a random number so that the loading values have random number?
Daniel
June 14, 2012
Question

How Would I Impliment This Into Anything?
DMCA.com