 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Toro Guest
|
Posted: Mon Mar 01, 2004 10:33 am Post subject: Swing Timer and utility Timer |
|
|
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
|
Posted: Mon Mar 01, 2004 11:33 pm Post subject: Re: Swing Timer and utility Timer |
|
|
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 |
|
 |
|
|
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
|
|