Java Swing Tutorial - Java Swing JProgressBar








A JProgressBar is used to display the progress of a task. It has an orientation, which can be horizontal or vertical.

A JProgressBar has three values associated with it: the current value, the minimum value, and the maximum value.

To create a horizontal progress bar with current, minimum, and maximum values set to 0, 0, and 100, respectively.

JProgressBar hpBar1  = new JProgressBar();

To create a horizontal progress bar with current, minimum, and maximum values set to 20, 20, and 200, respectively.

JProgressBar hpbar2  = new JProgressBar(SwingConstants.HORIZONTAL, 20,   200);

To create a vertical progress bar with current, minimum, and maximum values set to 5, 5 and 50, respectively.

JProgressBar vpBar1  = new JProgressBar(SwingConstants.VERTICAL, 5, 50);

To set the current value for the progress bar using its setValue(int value) method to indicate the progress.

To display a string that describes the progress bar's current value, use the setStringPainted(true) method and set the String to be painted by calling the setString(String s) method.

We can set a progress bar in an indeterminate mode using its setIndeterminate() method.

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
// www .  j a va2  s . c  o  m
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;

public class Main extends JFrame {
  JProgressBar bar = new JProgressBar();
  JButton step = new JButton("Step");

  public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    step.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        int value = bar.getValue() + 7;
        if (value > bar.getMaximum()) {
          value = bar.getMaximum();
        }
        bar.setValue(value);
      }
    });

    getContentPane().add(bar, BorderLayout.NORTH);
    getContentPane().add(step, BorderLayout.EAST);
    pack();
    setVisible(true);
  }

  public static void main(String arg[]) {
    new Main();
  }
}




JProgressBar with Timer

A progress bar is used for lengthy tasks.

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/*  w  w  w.j  ava 2s.c om*/
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.Timer;

public class Main {
  public static void main(String[] args) {
    final Timer  timer;
    final JProgressBar progressBar = new JProgressBar();
    final JButton button = new JButton("Start");
    JFrame f = new JFrame();
    f.setLayout(new FlowLayout());
    f.add(progressBar);
    f.add(button);

    ActionListener updateProBar = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        int val = progressBar.getValue();
        if (val >= 100) {
        //  timer.stop();
          button.setText("End");
          return;
        }
        progressBar.setValue(++val);
      }
    };
    timer = new Timer(50, updateProBar);
    button.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if (timer.isRunning()) {
          timer.stop();
          button.setText("Start");
        } else if (button.getText() != "End") {
          timer.start();
          button.setText("Stop");
        }
      }
    });
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setResizable(false);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}




Labeling a JProgressBar

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/*from   ww  w .  ja v  a2  s. c o  m*/
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;

public class Main {
  static class BarThread extends Thread {
    private static int DELAY = 500;

    JProgressBar progressBar;

    public BarThread(JProgressBar bar) {
      progressBar = bar;
    }

    public void run() {
      int minimum = progressBar.getMinimum();
      int maximum = progressBar.getMaximum();
      for (int i = minimum; i < maximum; i++) {
        try {
          int value = progressBar.getValue();
          progressBar.setValue(value + 1);

          Thread.sleep(DELAY);
        } catch (InterruptedException ignoredException) {
        }
      }
    }
  }

  public static void main(String args[]) {
    JFrame frame = new JFrame("Stepping Progress");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JProgressBar aJProgressBar = new JProgressBar(0, 50);
    aJProgressBar.setStringPainted(true);

    final JButton aJButton = new JButton("Start");

    ActionListener actionListener = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        aJButton.setEnabled(false);
        Thread stepper = new BarThread(aJProgressBar);
        stepper.start();
      }
    };
    aJButton.addActionListener(actionListener);
    frame.add(aJProgressBar, BorderLayout.NORTH);
    frame.add(aJButton, BorderLayout.SOUTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}

Virtical JProgressBar

To change the label to a fixed string, call the public void setString(String newValue) method and setStringPainted(true). On a vertical progress bar, the string is drawn rotated, so a longer string will fit better.

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/* w ww . j  ava2  s .c  o  m*/
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;

class BarThread extends Thread {
  private static int DELAY = 500;

  JProgressBar progressBar;

  public BarThread(JProgressBar bar) {
    progressBar = bar;
  }

  public void run() {
    int minimum = progressBar.getMinimum();
    int maximum = progressBar.getMaximum();
    for (int i = minimum; i < maximum; i++) {
      try {
        int value = progressBar.getValue();
        progressBar.setValue(value + 1);

        Thread.sleep(DELAY);
      } catch (InterruptedException ignoredException) {
      }
    }
  }
}

public class Main {

  public static void main(String args[]) {
    JFrame frame = new JFrame("Stepping Progress");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JProgressBar aJProgressBar = new JProgressBar(JProgressBar.VERTICAL);
    aJProgressBar.setStringPainted(true);

    final JButton aJButton = new JButton("Start");

    ActionListener actionListener = new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        aJButton.setEnabled(false);
        Thread stepper = new BarThread(aJProgressBar);
        stepper.start();
      }
    };
    aJButton.addActionListener(actionListener);
    frame.add(aJProgressBar, BorderLayout.NORTH);
    frame.add(aJButton, BorderLayout.SOUTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}

Indeterminate JProgressBar

Indeterminate mode: Progress bar bounces back and forth from side to side.

import java.awt.BorderLayout;
// w w  w  .java 2 s .co m
import javax.swing.JFrame;
import javax.swing.JProgressBar;

public class Main {

  public static void main(String args[]) {
    JFrame frame = new JFrame("Stepping Progress");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JProgressBar aJProgressBar = new JProgressBar(JProgressBar.VERTICAL);
    aJProgressBar.setStringPainted(true);
    aJProgressBar.setIndeterminate(true);

    frame.add(aJProgressBar, BorderLayout.NORTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}

Value Changes in a JProgressBar

Listening for Value Changes in a JProgressBar Component

import javax.swing.JProgressBar;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/*from w  ww . ja  v  a2s .  c om*/
public class Main {
  public static void main(String[] argv) throws Exception {
    int minimum = 0;
    int maximum = 100;
    JProgressBar progress = new JProgressBar(minimum, maximum);

    progress.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent evt) {
        JProgressBar comp = (JProgressBar) evt.getSource();
        int value = comp.getValue();
        int min = comp.getMinimum();
        int max = comp.getMaximum();
      }
    });
  }
}