AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Swing Timer and utility Timer

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
Toro
Guest





PostPosted: Mon Mar 01, 2004 10:33 am    Post subject: Swing Timer and utility Timer Reply with quote



Hi, I have a Swing application and want to use a Timer class to refresh the
statistics (using JTextArea) repeatedly at certain interval. I only need 1
timer and the refreshing must be very fast (as fast as 1 milli-second if
possible).

But I don't need an ActionListener as in Swing Timer.

And I read something on the web saying "The most important difference
between javax.Swing.Timer and java.util.Timer is that the latter doesn't run
its tasks on the event-dispatching thread."

So should I choose the Swing Timer or the utility Timer?

If I should choose the Swing Timer, just how do I deal with the
ActionListener? Passing a "null" to it? Please help me out using the
following as an example.

.....
.....

timer = new Timer(ONE_SECOND, new ActionListener() {
public void actionPerformed(ActionEvent evt) {

//...Update the statistics...

if (/* thread is done */) {
timer.stop();
//...Update the GUI...
}
}
});


Back to top
Knute Johnson
Guest





PostPosted: Mon Mar 01, 2004 11:33 pm    Post subject: Re: Swing Timer and utility Timer Reply with quote



Toro wrote:

Quote:
Hi, I have a Swing application and want to use a Timer class to refresh the
statistics (using JTextArea) repeatedly at certain interval. I only need 1
timer and the refreshing must be very fast (as fast as 1 milli-second if
possible).

But I don't need an ActionListener as in Swing Timer.

And I read something on the web saying "The most important difference
between javax.Swing.Timer and java.util.Timer is that the latter doesn't run
its tasks on the event-dispatching thread."

So should I choose the Swing Timer or the utility Timer?

If I should choose the Swing Timer, just how do I deal with the
ActionListener? Passing a "null" to it? Please help me out using the
following as an example.

....
....

timer = new Timer(ONE_SECOND, new ActionListener() {
public void actionPerformed(ActionEvent evt) {

//...Update the statistics...

if (/* thread is done */) {
timer.stop();
//...Update the GUI...
}
}
});



Most Swing components need to be updated from the event dispatch thread.
However the setText() and append() methods of JTextArea are thread
safe and don't need to be called from the EDT. So either timer would
work in this case. You will of course have a problem updating the
JTextArea on 1ms intervals. I can't imagine why you would need to do
that though. You certainly can't read text, even small amounts, at that
speed.

--

Knute Johnson
email s/nospam/knute/
Molon labe...

Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.