0

I'm trying to display the text of Successful Login before a system sleep for 3,000 miliseconds. Its not working when I place it right after the set text. How do I get it to display then pause so there is a bit of delay so the user knows that they loging in?

After the user correctly logs-in it will continue to a different class where the JFrame will close

l_Message.setForeground(Color.green);
l_Message.setText("Succesful Login");

try{
    Thread.sleep(3000);
} catch(InterruptedException ex) {
    Thread.currentThread().interrupt();
}

PLOGIN post_login = new PLOGIN();
post_login.postlogin_UI(login_JFrame);
dumbPotato21
  • 5,669
  • 5
  • 21
  • 34
D Rodriguez
  • 41
  • 1
  • 7
  • 2
    Don't use Thread.sleep(), the GUI can't repaint itself. Instead, just use a `Swing Timer` to schedule your activity after 3 seconds. See [How to Use Swing Timers](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) for more information. – camickr May 17 '17 at 20:15
  • 1
    Do not sleep on [EDT](http://stackoverflow.com/questions/7217013/java-event-dispatching-thread-explanation). – PM 77-1 May 17 '17 at 20:17
  • what if the login is not success? – Youcef LAIDANI May 17 '17 at 20:17
  • @YCF_L It just sets the text without calling a class – D Rodriguez May 17 '17 at 20:24

2 Answers2

2

See Concurrency in Swing for the reason why you're having problems

See How to use Swing Timers for a possible solution

import javax.swing.Timer

//...

l_Message.setForeground(Color.green);
l_Message.setText("Succesful Login");
Timer timer = new Timer(3000, new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        PLOGIN post_login = new PLOGIN();
        post_login.postlogin_UI(login_JFrame);
    }
});
timer.start();
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
-1

Assuming that you are calling this from outside of the GUI thread(which I believe that you should be), you could try the following:

    EventQueue.invokeLater(() -> {
        l_Message.setForeground(Color.green);
        l_Message.setText("Succesful Login");
    });

    try{
        Thread.sleep(3000);
    } catch(InterruptedException ex) {
        Thread.currentThread().interrupt();
    }

    PLOGIN post_login = new PLOGIN();
    post_login.postlogin_UI(login_JFrame);

i.e. schedule GUI operations to the GUI thread

munHunger
  • 2,572
  • 5
  • 34
  • 63
  • 1
    Since you're (probably) blocking the EDT any way, `invokeLater` won't get to run, because, the `Runnable` is scheduled to run on the EDT...later – MadProgrammer May 17 '17 at 20:31
  • Why would you block the EDT? – munHunger May 17 '17 at 20:36
  • 1
    `Thread.sleep` will block the EDT, this will prevent it from processing the Event Queue, into which you've posted the `Runnable`, so it won't get run until `Thread.sleep` returns, so, nothing here is fixing the issue that the OP is having – MadProgrammer May 17 '17 at 20:38
  • No. Thread.sleep will only block the current thread. If run on the EDT then what you are saying is correct. If this would not be correct then you could never run any EventQueue operations if any thread in the program is sleeping. I assumed in my answer(although maybe poorly phrased) that OP is not running on this thread, and thus this should work – munHunger May 17 '17 at 20:44
  • 1
    So, you've not said that, also, now you're suggesting that the OP violate the single threaded nature of Swing instead - as general rule, `Thread.sleep` is best avoided in Swing – MadProgrammer May 17 '17 at 21:15