Te dejo un ejemplo:
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
@SuppressWarnings("serial")
public class ProgressBar
extends JFrame {
private int c = 0;
// 5 minutos en milisegundos
private final int waitingTime = 5 * 60 * 1000;// 900000 milliseconds
private final int delay = waitingTime / 100;// 9000 == 9 seconds
public ProgressBar()
{
// delay in milliseconds
// inside the internal anonymous class we write the code we want the timer to execute each delay
// Instead of 50 milliseconds, you just have to write delay
@Override
{
if(c <= 100) pBar.setValue(++c);
}
});
// Adding an action listener: when you click the button, the timer starts
@Override
{
// starting the timer, which causes it to sending ActionEvents to its Listeners
t.start();
}
});
add(btn);
// Setting the initial value of the progress bar
pBar.setValue(c);// c == 0
// Showing a string progress bar
pBar.setStringPainted(true);
// adding a changeListener to the progress bar
@Override
{
if(pBar.getValue() == 100)
{
t.stop();
JOptionPane.
showMessageDialog(null,
"Your order is ready!");
c = 0;
pBar.setValue(c);// set the initial value of the progress bar to 0 again
}
}
});
add(pBar);
pack();
setDefaultCloseOperation
(JFrame.
EXIT_ON_CLOSE); setVisible(true);
setLocationRelativeTo(null);
}
public static void main
(String[] args
) {
public void run()
{
try
{
new ProgressBar();
}
{
}
}
});
}
}
Tienes que presionar el botón para que los 5 minutos empiecen a contar.
El código lo copié y modifiqué de
stackoverflow.
Saludos.