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 6850773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:09:58+00:00 2026-05-27T01:09:58+00:00

I’m trying to flash a Java graphic object by changing the color and calling

  • 0

I’m trying to flash a Java graphic object by changing the color and calling the repaint() method. The color is only updating with the final change color call. Here is my code:

public void start() {
    try {
        Color origColor = node.getColor();
        for (int i=0; i<noOfFlashes; i++) {
            Manager.gui.getDrawGraph().changeNodeColor(node, Color.WHITE);
            Thread.sleep(500);
            Manager.gui.getDrawGraph().changeNodeColor(node, origColor);
            Thread.sleep(500);
        }
        Manager.gui.getDrawGraph().changeNodeColor(node, Graph.VISITED_NODE);       
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

and the change node color method is:

public void changeNodeColor(Node node, Color c) {
    node.setColor(c);
    repaint();
}

The change node color is in the same class as the paint component.

Any help would be much appreciated.

  • 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-05-27T01:09:59+00:00Added an answer on May 27, 2026 at 1:09 am

    You need to use separate thread to manage your GUI event.
    You can do this, using a SwingWorker, as suggested by Amine, or implement the Runnable interface, or extend the Thread class, developing the run() method, that is the task of your thread.

    You can read this old question of SO : How do I use SwingWorker in Java?

    A tutorial for SwingWorker : http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html

    A tutorial to make a Thread : http://docs.oracle.com/javase/tutorial/essential/concurrency/

    The color is only updating with the final change color call.

    If you don’t use a separate thread, your gui will freezing until the method is completely executed, and you won’t see the color change separated by Thread.sleep(500);.

    UPDATE

    In this link, in the paragraph Why does a Swing GUI freeze or lock up?, you can understand why a Java Swing GUI freezes, with the use of a single thread.

    Check also this official link, in the paragraph Creating Threads, and this page, that returns:

    Swing’s single-thread rule says that Swing components can only be
    accessed by a single thread. This rule applies to both gets and sets,
    and the single thread is known as the event-dispatch thread.

    The single-thread rule is a good match for UI components because they
    tend to be used in a single-threaded way anyway, with most actions
    being initiated by the user. Furthermore, building thread safe
    components is difficult and tedious: it’s a good thing not to be doing
    if it can be avoided. But for all its benefits, the single-thread rule
    has far-reaching implications.

    Swing components will generally not comply with the single-thread rule
    unless all their events are sent and received on the event-dispatch
    thread. For example, property-change events should be sent on the
    event-dispatch thread, and model-change events should be received on
    the event-dispatch thread.

    For model-based components such as JTable and JTree, the single-thread
    rule implies that the model itself can only be accessed by the
    event-dispatch thread. For this reason, the model’s methods must
    execute quickly and should never block, or the entire user interface
    will be unresponsive.

    I think that the sentences above are very useful to understand better the Swing package.

    I report the suggestion of trashgod.

    You can use the Timer class, from the javax.swing.Timer package. That is also a good alternative.

    In this question, trashgod reports some examples of Timer.

    Check here for a tutorial about Timer.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
i got an object with contents of html markup in it, for example: string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post

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.