Java Swing How to - Move JProgressBar by step








Question

We would like to know how to move JProgressBar by step.

Answer

/*from  www.  j a va  2s  . c o m*/
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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();
  }
}