English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

How to make a delay in java?
I have currently
private void delay(int wait)
{
try
{
Thread.sleep(wait);
}
catch(InterruptedException e)
{
}
}
with the driver using 'delay(Any Number);'
My JFrame freezes, without it it does not, but is too quick to veiw... what is wrong?!

2007-02-20 07:09:45 · 2 answers · asked by YahooAnswers 2 in Computers & Internet Programming & Design

No errors. According to the System out print line

2007-02-20 07:35:44 · update #1

2 answers

Is the calling thread the Swing Event Thread? In other words, is the delay() method called by code that is triggered by the GUI? If so, you are blocking the GUI thread, thus making it "unresponsive".

If you have a window (JFrame, JDialog, etc.) that your code opens to display and then automatically dismisses, then you need a timer to trigger the action to dismiss the window, but without blocking the Swing Thread.

To do this, use the javax.swing.Timer class. See this tutorial:
http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html

2007-02-22 17:56:04 · answer #1 · answered by vincentgl 5 · 0 0

Put in the "catch" block:

catch(InterruptedException e)
{
System.out.println("Exception err: "+e);
}

To find out if there is an error. Without looking at the rest of your code, is the wait time in the thousands? Example, 1000 = 1 second because of the millisecond pause. Additionally, are you addressing the right thread?

The other option is to:

You could look into JOptionPane. Try, say,

String someString = JOptionPane.showInputDialog("Enter someString");

This will prompt the user before entering someString in a text box. You'll need to import javax.swing.JOptionPane; or javax.swing.*; if you're going to use this. In order to make this work for "tapping any key", insert the line above wherever you wish a pause.

That's all I got without anymore information.

2007-02-20 15:28:25 · answer #2 · answered by RadMatt 2 · 1 0

fedest.com, questions and answers