Tuesday, 13 October 2015

How To Use Progress Bar In Java

In java swing progress bar is a component that is used to display how much task have been performed or how much task has to be performed.In this tutorial progress bar value is increased after every 0.1 second and it has 100 stages so it shows that task will end in 10 seconds the time division can be changed using sleep method as shown below.

Sleep(long millis):

It uses to causes the current executing thread to sleep   milliseconds specified by a user.It throws  interuppted exception and it takes milliseconds as a input parameter. 

CODING:

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

class tab extends JFrame
{

JProgressBar jp;

tab()
{
setTitle("Progress Bar ");
setLayout(new FlowLayout());
setSize(900,200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jp = new JProgressBar();
jp.setStringPainted(true);
add(jp);
for(int i=0;i<=100;i+=1)
{
try{
Thread.sleep(100);
}
catch(InterruptedException ie)
{}
jp.setValue(i);
}
jp.setString("Done");
}
}

class progress {
public static void main(String[] args)
 {

tab x = new tab();
}
}

OUTPUT:




No comments:

Post a Comment