Progress indicators or progress bars are the new controls that give the users some indications of the progress of an operation. When we call JProgressBar, we can set the maximum and minimum value for the progress bar.
A JProgressBar is a Swing component that indicates progress. A ProgressMonitor is a dialog box that contains a progress bar. A ProgressMonitorinputStream displays a progress monitor dialog box while the stream is read. A progress bar is a simple component just a rectangle that is partially filled with color to indicate the progress of an operation. By default, progress is indicated by a string “n%”.
You construct a progress bar much as you construct a slider, by supplying the minimum and maximum value and an optional orientation:
progressBar = new JProgressBar(O, 1000);
progressBar = new JProgressBar(SwingConstants.VERTICAL, 0, 1000);
A progress bar is a simple component that can be placed inside a window. In contrast, a ProgressMonitor is a complete dialog box that contains a progress bar (see Fig below). The dialog box contains a Cancel button. If you click it, the monitor dialog box is closed.
The example below shows a simple application for creating a progress bar
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class ProgressBar extends JFrame
{
JProgressBar current;
JTextArea ta;
int num = 0;
public ProgressBar()
{
Container pane=getContentPane();
ta=new JTextArea("");
pane.setLayout(new GridLayout());
current = new JProgressBar(0, 100);
current.setValue(0);
current.setStringPainted(true);
pane.add(current);
pane.add(ta);
}
public void iterate()
{
while (num <= 100)
{
current.setValue(num);
try
{
Thread.sleep(500);
}
catch (InterruptedException e) { }
num += 10;
if (num>100)
{
break;
}
else
ta.setText(num+"%completed");
}
}
public static void main(String[] args)
{
ProgressBar pb = new ProgressBar();
pb.pack() ;
pb.setVisible(true);
pb.iterate ( );
}
}