I have a loop which shows an input dialog, process it and shows the name and picture of the student using setText and setIcon. After, I want to have at least 1-2secs of delay/pause so the user can have a look on it before it loops back to show the input dialog again on the screen.

while(true){
String box = JOptionPane.showInputDialog(null, "Scan Barcode: ");
	    				
	    				if(box == null){
	    					break;
	    				
	    				}else if(box.isEmpty()){
	    					JOptionPane.showMessageDialog(null, "Invalid Input. Please Try Again.", "Error" ,JOptionPane.ERROR_MESSAGE);
	    					
	    				}else{
                                          // show the student info and picture

                                        }
                                        
                                        //DELAY HERE before it loops back

}

I've tried Thread.sleep(n) but it seems to be working. It pauses the WHOLE program for a certain time and shows the student info and picture AND the dialog box AT THE SAME TIME. I also tried timer.schedule() , still no luck.

Thanks for the help.

Recommended Answers

All 7 Replies

I want to have at least 1-2secs of delay/pause so the user can have a look on it before it loops back to show the input dialog

Can you explain what you want to happen?
What is being shown on the GUI for the 1-2 seconds?
Do you want to have the program hide the display after that short a time? You just want it to quickly flash and then disappear?
Then what happens?
Is another one shown? When does it go away?

What is the rest of the program doing while the JOptionPane messages are being shown?

It's a cash register program. A dialogbox pops up and the cashier scans the barcode of each student's id then shows all the student's info(name, student number, etc) on the screen.

Now, I just want to have a delay before that dialogbox reappears again.

A delay AFTER the cashier closes the dialog box showing the student's info?
Why not go directly to asking for the next item?
What was the problem with the Thread sleep()?

Member Avatar for hfx642

Look up TIMER. I think that it will help with your problem.

Not sure how a Timer would help. His loop asks a question, then shows data and then loops back to ask the question again. The loop exits on a user's response.

I have no idea why you'd want to wait between the user's closing of the display of data before asking the user the question again.

you could put a boolean variable in for the condition and then set it to false when you want the delay, then start a timer that generates every 2 seconds, then you want to stop the timer set the variable to true and then call your loop again. You would probably do this by creating a method called something like getStudentInfo();

you could either pass the boolean, or make it a private variable all in the same class.

it would look like this

getStudentInfo (){
// do the stuff you want

bool = false;
timer.start;
}


and the timer would look like so

public void actionPerformed (ActionEvent e){
timer.stop;
bool = true;
getStudentInfo();
}

@NormR1 javax.swing.timer is correct suggestion, for clarifications, :-) "don't use Thread.sleep(int) in Swing code, don't use Thread.sleep(int) outside Thread, Executor ... :-), because in Swing code block EDT, and if there runs some BackGround Task/Thread/SwingWorker on separate thread then you have to calculate with unexpected output to the GUI by using Thread.sleeep(int)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.