Creating a modal progress dialog : ProgressBar « Swing JFC « Java






Creating a modal progress dialog

  

import java.awt.BorderLayout;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;

public class Main {
  public static void main(String[] args) {
    JFrame parentFrame = new JFrame();
    parentFrame.setSize(500, 150);
    JLabel jl = new JLabel();
    jl.setText("Count : 0");

    parentFrame.add(BorderLayout.CENTER, jl);
    parentFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    parentFrame.setVisible(true);

    final JDialog dlg = new JDialog(parentFrame, "Progress Dialog", true);
    JProgressBar dpb = new JProgressBar(0, 500);
    dlg.add(BorderLayout.CENTER, dpb);
    dlg.add(BorderLayout.NORTH, new JLabel("Progress..."));
    dlg.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
    dlg.setSize(300, 75);
    dlg.setLocationRelativeTo(parentFrame);

    Thread t = new Thread(new Runnable() {
      public void run() {
        dlg.setVisible(true);
      }
    });
    t.start();
    for (int i = 0; i <= 500; i++) {
      jl.setText("Count : " + i);
      dpb.setValue(i);
      if(dpb.getValue() == 500){
        dlg.setVisible(false);
        System.exit(0);
        
      }
      try {
        Thread.sleep(25);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
    dlg.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.ProgressBar Demo 2ProgressBar Demo 2
14.Use an actual input file to monitor rather than inducing progress manually
15.A demonstration of the ProgressMonitor toolbarA demonstration of the ProgressMonitor toolbar
16.A demonstration of the JProgressBar componentA demonstration of the JProgressBar component
17.Progress bar SampleProgress bar Sample
18.Sample ProgressSample Progress
19.ProgressBar StepProgressBar Step
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.