javacodex.com
Java Examples
Java Examples
memu home questions

JProgressBar

Sometimes a task running within a program might take a while to complete. A user-friendly program provides some indication to the user that the task is occurring, how long the task might take, and how much work has already been done. One way of indicating work, and perhaps the amount of progress, is to use an animated image.

A JProgressBar is used to display the progress of a task. It has an orientation, which can be horizontal or vertical. It has three values associated with it: the current value, the minimum value, and the maximum value.

Source: (MyProgress.java)

 
import javax.swing.*;
 
public class MyProgress extends JFrame {
 
  JProgressBar jb;
  int i = 0, num = 0;
 
  MyProgress() {
    jb = new JProgressBar(0, 2000);
    jb.setBounds(40, 40, 200, 30);
 
    jb.setValue(0);
    jb.setStringPainted(true);
 
    add(jb);
    setSize(300, 200);
    setLayout(null);
  }
 
  public void iterate() {
    while (i <= 2000) {
      jb.setValue(i);
      i = i + 20;
      try {
        Thread.sleep(150);
      } catch (Exception e) {
      }
    }
  }
 
  public static void main(String[] args) {
    MyProgress m = new MyProgress();
    m.setVisible(true);
    m.iterate();
  }
}
 

Output:

$ java MyProgress
Output



Contact: javacodex@yahoo.com