The reason you are better off using the swing Timer as opposed to the java.util.Timer is the swing timer runs the action event listeners on the Event Dispatch Thread so you can directly manipulate swing components.
I thought you wanted to wait 2 seconds and not 50 milliseconds!
You need to pass an ActionListener into the Timer's constructor.
You are passing 'this' which means when the timer completes it will call this objects actionPerformed method which creates and starts a new timer again and so on and so on.
BTW This is a good place to use an anonymous inner class ie
By default Timers repeat every n milliseconds so before starting it you should call
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
4
posted
0
Tony Docherty wrote: . . .This is a good place to use an anonymous inner class . . .
As a general rule, I like anonymous classes, provided you only use them once each. I do not like addActionListener(this), because it often denotes failure to use proper object design.
Campbell Ritchie wrote:I do not like addActionListener(this), because it often denotes failure to use proper object design.
Agreed, especially when the actionPerformed method has a list of if-else statements which take some action depending on which component triggered the event.
Tony Docherty wrote:I thought you wanted to wait 2 seconds and not 50 milliseconds!
You need to pass an ActionListener into the Timer's constructor.
You are passing 'this' which means when the timer completes it will call this objects actionPerformed method which creates and starts a new timer again and so on and so on.
BTW This is a good place to use an anonymous inner class ie
By default Timers repeat every n milliseconds so before starting it you should call
thanks... my code is working well now....
but as i am a beginner i am having some confusion with the Timer.....
can you share some good and easy article for it to learn....
thanks...
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
4
posted
0
I am pretty sure there is something about Timers in the Java Tutorials. Yes, try here.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32599
4
posted
0
Tony Docherty wrote: . . . when the actionPerformed method has a list of if-else statements . . .
And if that isn’t non‑object‑oriented design, I don’t know what is!
Michael Dunn wrote:> Can you explain me why have you made String originalText as final....
remove the word 'final', recompile, see what happens.
yes i removed and tried it gives the follwoing error message:
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - local variable originalText is accessed from within inner class; needs to be declared final
at EventDemo$1.actionPerformed(EventDemo.java:41)
at javax.swing.Timer.fireActionPerformed(Timer.java:312)
at javax.swing.Timer$DoPostEvent.run(Timer.java:244)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
"local variable originalText is accessed from within inner class; needs to be declared final" please explain me this line...