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.

Results 1 to 2 of 2

Thread: Making an object have a timer - needing help

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Making an object have a timer - needing help

    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.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Making an object have a timer - needing help

    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.)

Similar Threads

  1. Making a countdown timer using the currentTimeMillis method
    By Michael_D in forum Loops & Control Statements
    Replies: 10
    Last Post: June 6th, 2014, 12:02 PM
  2. Swing timer, falling object
    By ICEPower in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 21st, 2013, 09:50 AM
  3. Beginners needing help
    By Govna242 in forum Object Oriented Programming
    Replies: 8
    Last Post: February 28th, 2013, 06:26 PM
  4. Newbie needing a little help
    By Bgriesh in forum Loops & Control Statements
    Replies: 3
    Last Post: September 24th, 2012, 07:19 PM
  5. New student needing help!
    By newstudent in forum Object Oriented Programming
    Replies: 2
    Last Post: January 30th, 2012, 12:49 AM