This program demonstrates the use of a progress bar to monitor the progress of a thread. : ProgressBar « Swing JFC « Java

This program demonstrates the use of a progress bar to monitor the progress of a thread.

This program demonstrates the use of a progress bar to monitor the progress of a thread.
   This program is a part of the companion code for Core Java 8th ed.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <>.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;

 * This program demonstrates the use of a progress bar to monitor the progress of a thread.
 * @version 1.04 2007-08-01
 * @author Cay Horstmann
public class ProgressBarTest
   public static void main(String[] args)
      EventQueue.invokeLater(new Runnable()
            public void run()
               JFrame frame = new ProgressBarFrame();

 * A frame that contains a button to launch a simulated activity, a progress bar, and a text area
 * for the activity output.
class ProgressBarFrame extends JFrame
   public ProgressBarFrame()

      // this text area holds the activity output
      textArea = new JTextArea();

      // set up panel with button and progress bar

      final int MAX = 1000;
      JPanel panel = new JPanel();
      startButton = new JButton("Start");
      progressBar = new JProgressBar(0, MAX);

      checkBox = new JCheckBox("indeterminate");
      checkBox.addActionListener(new ActionListener()
            public void actionPerformed(ActionEvent event)
      add(new JScrollPane(textArea), BorderLayout.CENTER);
      add(panel, BorderLayout.SOUTH);

      // set up the button action

      startButton.addActionListener(new ActionListener()
            public void actionPerformed(ActionEvent event)
               activity = new SimulatedActivity(MAX);

   private JButton startButton;
   private JProgressBar progressBar;
   private JCheckBox checkBox;
   private JTextArea textArea;
   private SimulatedActivity activity;

   public static final int DEFAULT_WIDTH = 400;
   public static final int DEFAULT_HEIGHT = 200;
   class SimulatedActivity extends SwingWorker<Void, Integer>
       * Constructs the simulated activity that increments a counter from 0 to a
       * given target.
       * @param t the target value of the counter.
      public SimulatedActivity(int t)
         current = 0;
         target = t;

      protected Void doInBackground() throws Exception
            while (current < target)
         catch (InterruptedException e)
         return null;

      protected void process(List<Integer> chunks)
         for (Integer chunk : chunks)
            textArea.append(chunk + "\n");
      protected void done()
      private int current;
      private int target;


Related examples in the same category

1.Create a ProgressBar
2.Create a horizontal progress bar
3.Create a vertical progress bar
4.Creating a JProgressBar Component with an Unknown Maximum
5.Set all the values at once by using the model
6.Listening for Value Changes in a JProgressBar Component
7.Displaying the Percentage Done on a JProgressBar Component
8.Getting and Setting the Values of a JProgressBar Component
9.ProgressBar Demo: long taskProgressBar Demo: long task
10.When information on the task's progress is available, the progress bar can be made determinate:
11.Implement a progressbar in your application
12.ProgressMonitor DemoProgressMonitor Demo
13.Creating a modal progress dialog
14.ProgressBar Demo 2ProgressBar Demo 2
15.Use an actual input file to monitor rather than inducing progress manually
16.A demonstration of the ProgressMonitor toolbarA demonstration of the ProgressMonitor toolbar
17.A demonstration of the JProgressBar componentA demonstration of the JProgressBar component
18.Progress bar SampleProgress bar Sample
19.Sample ProgressSample Progress
20.ProgressBar StepProgressBar Step
21.ProgressBar DemoProgressBar Demo
22.Progressbar demo: set selection background, selection foreground and foregroundProgressbar demo: set selection background, selection foreground and foreground