Java Swing How to - Set progress for JProgressBar








Question

We would like to know how to set progress for JProgressBar.

Answer

/*from  w w  w. j  a  v a2 s. co m*/

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

public class Main extends JFrame {
  JProgressBar current = new JProgressBar(0, 2000);
  int num = 0;
  public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel pane = new JPanel();
    current.setValue(0);
    current.setStringPainted(true);
    pane.add(current);
    setContentPane(pane);
  }

  public void iterate() {
    while (num < 2000) {
      current.setValue(num);
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
      }
      num += 95;
    }
  }

  public static void main(String[] arguments) {
    Main frame = new Main();
    frame.pack();
    frame.setVisible(true);
    frame.iterate();
  }
}