Results 1 to 5 of 5

Thread: Java Timer

  1. #1
    yodaster is offline Member
    Join Date
    Nov 2015
    Posts
    2
    Rep Power
    0

    Default Java Timer

    Good afternoon,

    I am trying to create a program that is very similar to an old school cell phone. I want to enter text into a JTextArea by pressing JButtons. I am able to enter the text just fine but i need to incorporate a timer into it so that if i press a button one time and the timer cycles through one time (eg 2500ms) and it sets the char and i can hit the same button again and it starts the same process over again. I.E.

    if (currentbutton == "two")
    {
    timer.start();
    if(timer == 2500)
    {
    if(clickCount == 1)
    setChar = 'A';
    if(clickCount == 2)
    setChar = 'B';
    if(clickCount == 3)
    setChar = 'C';
    }
    timer.stop();
    so if i want to type the word about, i would hit the two button once and then wait and it would set the A and then reset the clickcounter and then hit it twice and then it will set the B.

    I hope this makes sense.

    Thanks

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Java Timer

    a timer into it so that if i press a button one time and the timer cycles through one time (eg 2500ms) and it sets the char
    Are you asking how to have a delay between when a user presses a button and when something is added to the textarea?

    Please wrap any posted code in code tags to preserve its formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    yodaster is offline Member
    Join Date
    Nov 2015
    Posts
    2
    Rep Power
    0

    Default Re: Java Timer

    I am not certain how to use code tags.

    And that is kind of what I am asking. If the user presses a button then the timer starts, once the timer has cycled one time, it sets that character and will then move on to the next character.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Java Timer

    Please edit your post and wrap your code with code tags:
    [code]
    **YOUR CODE GOES HERE**
    [/code]
    to get highlighting and preserve formatting.

    One idea would be a queue of future events and when they should execute. Set the timer for the initial delay, when the timer method executes, it removes and does the next event, then sets the timer's delay to the next event's time.
    For example (with 2.5 sec delay) if the user presses a button now, 2 seconds later and then again 2 seconds later, set would be called at 2.5 and again at 4.5 and again at 6.5 seconds.
    Last edited by Norm; 11-27-2015 at 03:27 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Java Timer

    Are you trying to recreate the MultiTap system? (https://en.wikipedia.org/wiki/Multi-tap)

    It's actually quite fun to make. I have made it using 12 JButtons that share the same ActionListener. The ActionListener passes the button and it's text to a helper class that contains a Timer. That helper is responsible for checking if the source is the same as the previous source:
    If it is the same source, then remember the next letter on that button and restart the timer. After the timer expires, print the letter and reset the state.
    If it is not the same source, print the current letter immediately, start remembering the new letter, and restart the timer. After the timer expires, print the letter and reset the state.

    It's not that hard, but it's not as trivial as "timer == 2500" though :)

    Edit: Changed T9 -> MultiTap. T9 is a whole different ballgame, but a very interesting challenge :)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Java Timer
    By Oliver in forum AWT / Swing
    Replies: 9
    Last Post: 09-06-2015, 04:08 AM
  2. Stopping a Timer from Inside the timer
    By krishnan in forum Java Applets
    Replies: 2
    Last Post: 10-04-2010, 11:15 PM
  3. Need Help with timer (Java)
    By acash229 in forum New To Java
    Replies: 58
    Last Post: 09-13-2010, 01:32 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:46 PM
  5. Help with timer in java
    By barney in forum Advanced Java
    Replies: 1
    Last Post: 08-01-2007, 10:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •