Set delay time in JOptionPane

In this section, you will learn how to set the time after which the message
should be displayed using JOptionpane. For this, first of all we prompt the user
to enter time in seconds and the message to display. After that using the
Thread.sleep() method we have allowed the message to be displayed after the
specific interval of time.
Here is the code:
import javax.swing.*;
public class SetDelayTime{
public static void main(String args[]){
try {
UIManager.setLookAndFeel(UIManager. getSystemLookAndFeelClassName () );
}
catch ( Exception e ) {}
int time = 0;
while(true) {
time = Integer.parseInt( JOptionPane.showInputDialog ( "Enter time in seconds" ));
break;
}
time *=1000;
String str = JOptionPane.showInputDialog( "Enter your message" );
if (str == null) return;
try {
Thread.sleep(time);
JOptionPane.showMessageDialog( null,str,"Alert", JOptionPane.ERROR_MESSAGE);
} catch (Exception e) {}
}
}
|
Download Source Code:

|