Using Swing Timer to insert a delay between append

I would like to insert a simple delay between the two append() in this code. I have looked through other posts but from what I could tell you need an action listener for the timer, but since I need to have the timer inside an existing action performed I didn't really know how to do.

I basically want a simple thread.sleep(500) like method that I can call whenever I want, but I don't want the whole application freezing..

JTextField charNameIn =new JTextField(16);

JTextArea advLog =new JTextArea(12, 16);

publicvoid actionPerformed(ActionEvent e){

if (e.getSource() == charNameIn){

advLog.append("text");

// insert a 500 ms pause here

advLog.append("text");

}

}

Message was edited by:

Lombra

[1132 byte] By [Lombraa] at [2007-11-27 3:13:12]
# 1

create a new Thread when actionPerformed starts like:

if (e.getSource() == charNameIn) {

Thread t=new Thread(){

private int counter=0;

public void run(){

while(counter<2){

printInADV("text");

try{

this.sleep(500);

}catch(InterruptedException i){}

counter ++;

}

}

private void printInADV(String str){

advLog.append(str);

}

};

t.start();

}

suparenoa at 2007-7-12 8:15:42 > top of Java-index,Desktop,Core GUI APIs...
# 2

That seemed to work ok. But it's not exactly what I wanted (mistake on my side decribing my problem). I didn't intentionally want "text" in both append(), so I would like it to works with any text string. Like

append("In two seconds you will get another message");

// sleep(2000)

append("Hi");

Lombraa at 2007-7-12 8:15:42 > top of Java-index,Desktop,Core GUI APIs...
# 3

So do something like

if (e.getSource() == charNameIn) {

Thread t=new Thread(){

private String message1 = "In two seconds you will get another message;

private String message2 = "Hi";

private long sleepTime = 2000;

public void run(){

while(counter<2){

advLog.append( message1 );

try{

this.sleep( sleepTime );

}catch(InterruptedException i){}

advLog.append( message1 );

}

}

};

t.start();

}

Of course, if you intend to put this into a textArea or such, you'll want to

do that part in the EDT.

JayDSa at 2007-7-12 8:15:42 > top of Java-index,Desktop,Core GUI APIs...
# 4

> That seemed to work ok. But it's not exactly what I

> wanted (mistake on my side decribing my problem). I

> didn't intentionally want "text" in both append(), so

> I would like it to works with any text string. Like

>

> append("In two seconds you will get another

> message");

> // sleep(2000)

> append("Hi");

if (e.getSource() == charNameIn) {

final String[] array={"In two second...","hi"};

Thread t=new Thread(){

private int counter=0;

public void run(){

while(counter<array.length){

printInADV(array[counter]);

try{

this.sleep(500);

}catch(InterruptedException i){}

counter ++;

}

}

private void printInADV(String str){

advLog.append(str);

}

};

t.start();

}

>

suparenoa at 2007-7-12 8:15:42 > top of Java-index,Desktop,Core GUI APIs...
# 5

Don't use a Thread.sleep in the ActionListener you will prevent the GUI from responding to events.

> but since I need to have the timer inside an existing action performed I didn't really know how to do

You create a separate ActionListener:

ActionListener someListener = new ActionListener()

{

public void actionPerformed(...)

{

// add your code here

}

}

Timer someTimer = new Timer(1000, someListener);

someTimer.start();

camickra at 2007-7-12 8:15:42 > top of Java-index,Desktop,Core GUI APIs...
# 6

I create it in the existing action performed like this? Tried it but got ; expected so I guess it should've been somewhere else. When I added the ; it wanted it would just add "Bye" infinitely.

public void actionPerformed(ActionEvent e)

{

if (e.getSource() == charNameIn)

{

ActionListener someListener = new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

advLog.append( "Bye");

}

}

Timer someTimer = new Timer(1000, someListener);

someTimer.start();

advLog.append( "Hi");

}

}

Lombraa at 2007-7-12 8:15:42 > top of Java-index,Desktop,Core GUI APIs...
# 7

ActionListener someListener = new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

advLog.append( "Bye");

}

}; //<-- You're missing a ; here

Torgila at 2007-7-12 8:15:42 > top of Java-index,Desktop,Core GUI APIs...
# 8
Yeah, I added that but then "Bye" was added every second and wouldn't stop.
Lombraa at 2007-7-12 8:15:42 > top of Java-index,Desktop,Core GUI APIs...
# 9
The Timer API introduction explains what you need to do: http://java.sun.com/javase/6/docs/api/javax/swing/Timer.html
Torgila at 2007-7-12 8:15:42 > top of Java-index,Desktop,Core GUI APIs...
# 10

Ah, right, forgot about the setRepeat method. This is great, thanks all! Dukes awarded.

One last question; how do I use the timer again for a third event? I could only think of creating a new action listener for that purpose, but is there another way?

advLog.append("Start");

ActionListener taskPerformer = new ActionListener()

{

public void actionPerformed(ActionEvent evt)

{

advLog.append("One second...");

}

};

Timer timer = new Timer(1000, taskPerformer);

timer.setRepeats(false);

timer.start();

ActionListener task2Performer = new ActionListener()

{

public void actionPerformed(ActionEvent evt)

{

advLog.append("Two seconds...");

}

};

Timer timer2 = new Timer(2000, task2Performer);

timer2.setRepeats(false);

timer2.start();

Lombraa at 2007-7-12 8:15:42 > top of Java-index,Desktop,Core GUI APIs...