Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9124585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:36:19+00:00 2026-06-17T06:36:19+00:00

This post is related to my last post.The block of code changes the text

  • 0

This post is related to my last post.The block of code changes the text to desired color after desired time.
however, now I want to change the color of a perticular word such that each letter gets the equal time.EG if “hello” have been given a time of 1000 milliseconds (have 5 letters) then ‘h”e”l”l”o’ each letter should get 1000/5 milliseconds i.e 200 milliseconds each.

I implemented swing timer for this :

public Reminder() {

    a[0]=2000;
    a[1]=1000;
    a[2]=3000;
   a[3]=5000;
    a[4]=3000;
ActionListener actionListener = new ActionListener() {
  public void actionPerformed(ActionEvent actionEvent) {
  point =point +arr[i].length();
i++;

     doc.setCharacterAttributes(0,point+1, textpane.getStyle("Red"), true);
     timer.setDelay(a[i]);

    }
};

timer = new Timer(a[i], actionListener);
timer.setInitialDelay(0);
timer.start();

For this to happen, shoud I use another Timer inside the actionListener to give further timings to a perticular letter?Or should I first break the time by .length()
and then use the timer?I cannot decide a better way.Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T06:36:21+00:00Added an answer on June 17, 2026 at 6:36 am

    You should never need more than one timer. Since you know ahead of time exactly when everything should happen, just calculate those times, put them in a list (sorted by time) and execute each one.

    public colorize(int offset, int length) {
        long triggerTime[] = new long[length];
        long startTime = System.currentTimeMillis();
        for (int i=0; i<length; i++) {
             triggerTime[i] = startTime + (1000*i)/length;
        }
    
        for (int i=0; i<length; i++) {
            //just wait for the next time to occur
            Thread.sleep(triggerTime[i]-System.currentTimeMillis());
            doc.setCharacterAttributes(offset, i+1, textpane.getStyle("Red"), true);
        }
    }
    

    This may strike you as very pedestrian because it does not use a TimerTask object, but it is effective, efficient, and easy to debug. You simplty call this method on whatever thread you want, and it occupies the entire thread, and the word will be colorized at a rate such it is completed in 1 second.

    You could, if you don’t have a thread handy, make a timer that calls this, but the only reason for that is to access a thread. The real point is: don’t set up multiple timers, just make an array of time values. After one event is satisfied, set to delay until the next time. You never need multiple timers.

    It would be a little cleaner if you made an object that represented the coloring of a character (or whatever action you want) and you put together a collection of these actions. Then sort the entire collection by time they are to go off. The loop above would walk through the collection, waiting until the time for the action arrives, and then executing it. Another advantage of this approach is that you could clear the collection and that would terminate the loop.

    See the discussion of the overuse of timers on my website to understand why this is bad.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is related to this post . I think I am having problem with
This question is related to this SO post Rather than using a recursive CTE
This question is related to my previous post Image Processing Algorithm in Matlab in
This question is related to the previous post. How to save file and read
This question is related to a previous post . Is there something comparable to
This is a follow-up question related to my previous post . Below is a
This question is related with one of my earlier questions.. Previous Post In there
I want to refer to this post, because it might relate: Make Form Fields
I have this code that should return all the posts that related to the
This Question is Related to my earlier post here: CSS loading issue with Android

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.