ProgressBar Step : ProgressBar « Swing JFC « Java






ProgressBar Step

ProgressBar Step
  
/*
Definitive Guide to Swing for Java 2, Second Edition
By John Zukowski     
ISBN: 1-893115-78-X
Publisher: APress
*/

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;

public class ProgressBarStep {
  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();
      Runnable runner = new Runnable() {
        public void run() {
          int value = progressBar.getValue();
          progressBar.setValue(value + 1);
        }
      };
      for (int i = minimum; i < maximum; i++) {
        try {
          SwingUtilities.invokeAndWait(runner);
          // our job for each step is to just sleep
          Thread.sleep(DELAY);
        } catch (InterruptedException ignoredException) {
        } catch (InvocationTargetException ignoredException) {
        }
      }
    }
  }

  public static void main(String args[]) {
    // Initialize
    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);

    String title = (args.length == 0 ? "Stepping Progress" : args[0]);
    JFrame theFrame = new JFrame(title);
    theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Container contentPane = theFrame.getContentPane();
    contentPane.add(aJProgressBar, BorderLayout.NORTH);
    contentPane.add(aJButton, BorderLayout.SOUTH);
    theFrame.setSize(300, 200);
    theFrame.setVisible(true);
  }
}

           
         
    
  








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 DemoProgressBar Demo
21.Progressbar demo: set selection background, selection foreground and foregroundProgressbar demo: set selection background, selection foreground and foreground
22.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.