Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making the user wait using Swing

Tags:

java

swing

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?

like image 502
user10101 Avatar asked Apr 27 '26 10:04

user10101


2 Answers

You can use javax.swing.Timer. For example:

enter image description here

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--;
        }
    }
}
like image 150
Eng.Fouad Avatar answered Apr 30 '26 17:04

Eng.Fouad


I think JProgressBar is what you need!..

like image 36
Shashank Kadne Avatar answered Apr 30 '26 17:04

Shashank Kadne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!