import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;

        // Show a JProgressBar and periodically update it.
class SwingProgressFrame extends JFrame implements Runnable {
    public SwingProgressFrame() {
        final int width = 300, height = 100;
        setSize(width,height);

        Container contents = getContentPane();
        contents.add(getBar(),"Center");
        contents.add(new QuitJPanel(),"South");
    }

        // Sleep for short periods and add the sleep time to the
        // value of the bar.
    public void run(){
        final int sleepTime = 100;
        // A formatter for the percentage progress
        NumberFormat formatter = NumberFormat.getNumberInstance();
        formatter.setMinimumFractionDigits(2);
        formatter.setMaximumFractionDigits(2);

        final JProgressBar bar = getBar();
        bar.setValue(0);
        // Don't assume some one else isn't changing the bar's data.
        while(bar.getValue() < bar.getMaximum()){
            try{
                Thread.sleep(sleepTime);
                bar.setValue(bar.getValue()+sleepTime);
                // Show what percentage has been done.
                double percentDone = 100*bar.getPercentComplete();
                bar.setString(formatter.format(percentDone)+"%");
            }
            catch(InterruptedException e){
            }
        }
    }

    protected JProgressBar getBar(){
        return bar;
    }

    // Let it run for the equivalent of 30 seconds.
    private final int min = 0, max = 30*1000;
    private final JProgressBar bar =
        new JProgressBar(JProgressBar.HORIZONTAL,min,max);
    {
        bar.setStringPainted(true);
    }
}
