Waiting for the user with Swing

I want the user to wait a certain amount of time (10 seconds). I know we are using META tag in JSPs or servlets <META HTTP-EQUIV="Refresh" CONTENT="3">

. Is there a way in Swing that we can make the user wait for a while. I am using Swing; I want the user to wait for a certain amount of time and I want to display some information to be fetched from the database. Is this possible through Swing?

+3


source to share


4 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--;
        }
    }
}

      

+5


source


Prior to WebSockets, HTTP servers could not dispatch events to HTTP clients; interactions were mostly request-response. Many applications are working on this problem using a polling (client-side) approach. The update meta tag is one way to implement a poll.

The swing is very different - you have the full force of the events. Thus, the idea of ​​making the user wait for a certain amount of time is usually a wrong interaction. (Some play / poll / animation are some of the exceptions where just waiting makes sense.)

You must develop a Swing GUI that is functional and responsive until the results have been computed / retrieved. After the results are available, update the model and fire the event that changed the model.



The model itself can perform background calculations, polls, etc. as needed; this code doesn't depend on swing. If you would like to help with this aspect, consider finding out / asking about it separately on Stack Overflow.

Finally, remember that sleeping on a swinging thread will make the UI unresponsive. And on a related note, any event triggered by your model should be queued to the Swing event stream. See SwingUtilities.invokeLater (...) for how this is done.

+3


source


I think JProgressBar is what you need! ..

+2


source


Have you tried themes. Using

   Thread.sleep(10000);

      

you can handle it easily. You can get more information here

+1


source







All Articles