Magazine
 
Tips ‘n’ Tricks
 



3. Creating Indeterminate Progress
Bar in Java:


Sometimes, at the time of downloading or transferring a file, we see a component showing how much the particular task has been completed. For this purpose, Java provides a Progress Bar component to convey the progress of completing a task in a GUI. One way of showing this progress is in percent format. But suppose a situation where extent of the task is unknown and so we are not able to present it in percent format. To handle this situation java provides a solution by putting the progress bar in indeterminate mode. An indeterminate progress bar animates constantly. By default, every progress bar is determinate. You may make any JProgressBar indeterminate pass a “true” boolean value to etIndeterminate() method. Just have a look on the code below:


IndeterminatePB.java:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class IndeterminatePB extends
JFrame {
JProgressBar ipb;
int number = 0;
public IndeterminatePB() {
super(“Indeterminate
Progress Bar Example”);
setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
ipb = new
JProgressBar(JProgressBar.HORIZONTAL,
0, 1000);

//make any JProgressBar indeterminate

 

passing a true boolean value to
setIndeterminate() method.
ipb.setIndeterminate(true);
pane.add(ipb);
setContentPane(pane);
}
public void iterate() {
while (number <= 1000) {
ipb.setValue(number);
//Sets the progress bar’s current value to n.
try {
Thread.sleep(10);
}
catch (InterruptedException e) { }
number++;
}

//To stop the animation make the progress bar determinate.

ipb.setIndeterminate(false);
}
public static void main(String[] args) {
IndeterminatePB frame = new
IndeterminatePB();
frame.pack();
frame.setVisible(true);
frame.iterate();
}
}

Compile and Run:

C:\JavaJazzup>javac IndeterminatePB.java
C:\JavaJazzup>java IndeterminatePB

Output:


The output below appears until the completion of the task. The dark portion waves from one side to the other.



When the task ends, progress bar appears like this.

Sept 2007 | Java Jazz Up | 68
previous
index
next
 
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,   Download PDF