Let me just update my post.
This is what I want.
1) Enter some text into JTextField
2) Press 'Enter' from JTextField
3) Show in JLabel some text
3) Stop (pause) for 1 seconds (Thread.sleep(1000))
4) Continue program
Now, JTextField has KeyListener with keyPressed method for ENTER key and when ENTER is pressed from keyPressed another method in another class is called. In that another class is code that show text in JLabel and after is Thread.sleep(1000) command. But that doesn't happened, it always shows text in JLabel after Thread.sleep command.
Why, how to fix that?
> I am sure you are interrupting the AWT Event
> Dispatcher Thread.
> You should NEVER interrupt this thread with a
> Thread.sleep since you can have undesired effects
> like the one that happens to you.
> The better solution is to make the wait in another
> thread.
How do you mean to make wait in another thread?
Explain it.
Use a javax.swing.Timer: http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html
demo:import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TextAreaExample implements Runnable, ActionListener {
private JTextArea area;
private String[] text = {
"This is the first line",
"This is the second",
"This is the last",
};
private int offset = 0;
public void run() {
area = new JTextArea(10,40);
JFrame f = new JFrame("TextAreaExample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(new JScrollPane(area));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
new Timer(1000, this).start();
}
public void actionPerformed(ActionEvent evt) {
if (offset < text.length) {
area.append(text[offset++]);
area.append("\n");
} else {
((Timer) evt.getSource()).stop();
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new TextAreaExample());
}
}
Any activity that takes more than a fraction of a second in a GUI environment (like an applet) needs to be run in a background thread.
You need to create an object which implements Runnable, then create a Thread object pointing to that runnable.
Then you call start() on the thread to set it going. start() returns immediately, but at the same time the run() method of the Runnable object starts executing in parallel.
You'll often use an anonymous class as in:
Runnable threadCode = new Runnable() {
public void run() {
... code which runs in background
}
};
Thread t = new Thread(threadCode);
t.start();
> I can't use Timer because there is other code after.
> If I use Timer then other code will continue to
> execute, I need a pause in execution.
You must not pause the dispatcher thread (the thread that calls your Applet callback methods and ActionListeners). If you do the whole interface freezes up and it won't repaint anything you change until the offending method returns.
Many applets have a background thread, which performs some protracted series of operations. Others have a Timer which steps through an animation at specified time intervals.
Let me just post this idea.
I have created a class Pause for pausing.
It is:
import java.util.Date;
public class Pause {
Pause(int seconds){
int timeSec;
Date dt = new Date();
int sec = dt.getSeconds();
do{
timeSec = dt.getSeconds();
}while(timeSec-sec<seconds);
}
}
In code it would be called:
Pause p = new Pause(2);
if I want 2 seconds pause.
But, this doesn't work.
Variable timeSec never changes even though it should because it checks seconds, but getSeconds() always returns same integer.
Any clue why is that happening?>
> Maybe if you described your process better.
>
> You write that you want to display some text, pause,
> then continue execution.
> What do you mean by "continue execution"?
There is some code after that displaying text.
It should continue normally after pause.
But for effective application I need that pause otherwise it's not good.
You could have a timer that invokes the ActionListener:
public void actionPerformed(ActionEvent evt) {
continueNormally();
}
I'm assuming the timer's setRepeats is false.
I have a question.
I have figured a way to make a pause by adding this code:
long s;
s=System.currentTimeMillis();
do{
}while(System.currentTimeMillis()-s<2000);
It pause for 2 seconds, but it still doesn't work.
Behavior is like I described with Thread.sleep. Commands before and after pause are executed all after pause.
That is not right, something is wrong.
Does someone understand this and can help me?