I want to make the user to wait for a certain amount of time (10 seconds). I know in JSP or in servlets we use the META tag <META HTTP-EQUIV="Refresh" CONTENT="3">. Is there any way in Swing that we can make the user wait for some time. I am using Swing; I want to make the user wait for certain amount of time, and I want to display some information that will be fetched from the database. Is it possible through Swing?
You can use javax.swing.Timer. For example:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
public class SimpleTimer extends JFrame implements ActionListener
{
private JLabel label;
private Timer timer;
private int counter = 10; // the duration
private int delay = 1000; // every 1 second
private static final long serialVersionUID = 1L;
public SimpleTimer()
{
super("Simple Timer");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(300, 65);
label = new JLabel("Wait for " + counter + " sec");
getContentPane().add(label);
timer = new Timer(delay, this);
timer.setInitialDelay(0);
timer.start();
setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new SimpleTimer();
}
});
}
@Override
public void actionPerformed(ActionEvent e)
{
if(counter == 0)
{
timer.stop();
label.setText("The time is up!");
}
else
{
label.setText("Wait for " + counter + " sec");
counter--;
}
}
}
I think JProgressBar is what you need!..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With