Display progress value in JProgressBar to a text field - Java Swing

Java examples for Swing:JProgressBar

Description

Display progress value in JProgressBar to a text field

Demo Code

import java.awt.FlowLayout;

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

public class Main extends JFrame {
  JProgressBar current;/*  www .  j a va 2 s.c o m*/
  JTextField numField;
  JButton find;
  Thread runner;
  int num = 0;

  public Main() {
    super("ProgressMonitor2");

    setSize(220, 120);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel pane = new JPanel();
    pane.setLayout(new FlowLayout());
    numField = new JTextField("" + num, 8);
    pane.add(numField);
    current = new JProgressBar(0, 2000);
    current.setValue(0);
    current.setStringPainted(true);
    pane.add(current);
    setContentPane(pane);
    setVisible(true);
  }

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

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

Related Tutorials