Sunday, 3 May 2015

How to Create Simple JProgress Bar In Java

Progress bar is a swing component use to display how much task have been completed.In this tutorial I have used four different methods of progress bar it can be used for different purposes as it is clear from program setMinimum() and setMaximum() value is used to set initial and final value of progress bar if we use setStringPainted(false) the percentage on progress bar will  be disappear. setValue() is used to set value of percentage of task have been completed. 

CONSTRUCTORS  OF PROGRESSBAR:

 JProgressBar()

By using this constructor we can create a progress bar with initial value of zero and ending value of  100.we can also set minimum and maximum value of progress bar by methods as we done in this program.

JProgressBar(int)

Creates a progress bar with orientation if user sets 1 vertical progress bar will create and if user set 0 horizontal progress bar will
create.
JProgressBar(int,int,int)

In this constructor first value determines the the orientation of progress bar and last two value specify the minimum and  maximum values of progress bar.

JProgressBar(int,int)

Starting and ending value of constructor can also be define by using this constructor.

CODING:

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

class prog extends JFrame 
{

JProgressBar my;

prog()
{
setTitle("JProgressBar");
FlowLayout f=new FlowLayout();
setLayout(f);
setSize(700,200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
my = new JProgressBar();
my.setMinimum(0);
my.setMaximum(100);
my.setValue(25);
my.setStringPainted(true);
my.setBackground(Color.CYAN);
add(my);

}


}

class myprogress {

public static void main(String[] args) 
{

prog fr = new prog();
}
}

OUTPUT:



No comments:

Post a Comment