I am trying to make a timer so when you click on an object the timer will start and then when the timer hits 0 it turns back to the original object.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
I am trying to make a timer so when you click on an object the timer will start and then when the timer hits 0 it turns back to the original object.
There are two sorts of timer commonly used: java.util.Timer and javax.swing.Timer. It sounds like what you are looking for is a Swing timer illustrated on the How to Use Swing Timers page of Oracle's Tutorial.
Basically the logic is:
* respond to a click event by (1) checking to see if the timer is already running, if so do nothing or (2) start the timer and change the object (possibly triggering a repaint if necessary)
* respond to a timer event by changing the object back again (and repainting if necessary)
Where the logic should be implemented might depend on just what sort of change you are making. If it's some simple thing (especially one that doesn't affect layout) like changing a background colour of a component, then the component itself could implement the logic:
* on click, change the background colour and start the timer
* on timer event, change the background colour back again
On the other hand if the component is going to radically change - say from being a label to being a text field - then the container might be a better place to implement the logic:
* on label click, start the timer, remove the label and add the text field
* on timer event, do whatever with the text field's contents, remove it and add the label back again.
(Such an interface with now-you-see-it now-you-don't components might annoy the user, but I give it as an illustration where the "work" might more naturally be done by the parent container.)