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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:51:25+00:00 2026-06-08T20:51:25+00:00

I am using java. I have a click event that adds squares to a

  • 0

I am using java.
I have a click event that adds “squares” to a container, in a loop. I want each square to show up RIGHT when it is added. I tried running the ‘adding of the squares’ in a separate thread, but it is not working.

Here is some code I use for ‘public class GuiController implements ActionListener, MouseListener’:

@Override
public void mouseClicked(MouseEvent e)
{
    //createBoardPane();
    new Thread
    (
        new Runnable() 
        {
            public void run() 
            {
                showAnimation();
            }
        }
    ).start();
}

public void showAnimation()
{   
    for(int i = 0; i < model.getAnimationList().size(); i++)
    {
        String coord = model.getAnimationList().get(i);
        int x = Integer.parseInt(coord.substring(0, coord.indexOf(',')));
        int y = Integer.parseInt(coord.substring(coord.indexOf(',') + 1, coord.length() - 2));
        boolean shouldPlacePiece = (coord.charAt(coord.length() - 1) == 'p');

        if(shouldPlacePiece)
        {
            model.getView().getBoardPane().getComponent(x + (y * model.getBoardSize())).setBackground(Color.BLACK);
        }
        else
        {
            model.getView().getBoardPane().getComponent(x + (y * model.getBoardSize())).setBackground(Color.WHITE);
        }

        model.getView().getBoardPane().repaint();

        long time = System.currentTimeMillis();
        while((System.currentTimeMillis() - time) < 250)
        {
            // wait loop
        }
    }
}

any help is 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-06-08T20:51:27+00:00Added an answer on June 8, 2026 at 8:51 pm

    Creating a separate Thread to run for this longish-running task was an excellent idea – unless you want to lock-up interactions with your GUI while doing your animation.

    Now, Swing GUI objects are not Thread safe (with few exceptions), so you cannot work with them from a thread other than Swing’s Event Dispatch Loop’s thread. So take all the GUI update code in your for-loop, and wrap it with a new Runnable (yes, another one).
    Then call SwingUtilities.invokeLater(Runnable doRun) with that Runnable on each iteration of the loop.

    Your GUI update code will then be scheduled to run ASAP on the Event Dispatch Loop, which will occur while your worker thread goes to sleep (do you have anything against Thread.sleep?).

    Alternative: Use SwingWorker instead of Thread

    SwingWorker will create and manage a new the Thread for you, and publish data that it (SwingWorker) will cause to be run on the Event Dispatch Loop’s thread. You’ll override doInBackground with your code. Call publish with parameters to push across into the Event Dispatch Thread. Override process with code to process those parameters and update your GUI.

    On gotcha with SwingWorker is that it accumulates published events over a period of about 33 milliseconds. If you’re publishing more frequent than that you may get all your events bunched together every 33 milliseconds or so. In you case, 250 milliseconds between updates shouldn’t be a problem.

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

Sidebar

Related Questions

I have built a web application using Java EE platform that sells one of
I have created a simple web-service using Java. i want to load jars related
I am using group radio buttons.What i want is that when i have selected
I have a java swing App that connects to a remote Unix box using
Short: I need a right-click event to highlight the cell row. I am using
I am using java and have to deal with numbers larger than long (which
I am using POI 3.8 to create Excel sheets using Java I have created
I have a Java based web-application using Java Server Faces and Facelets. I am
I have certificate created using java class CertAndKeyGen and X500Name and I am able
I have to write an XML file using java.The contents should be written from

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.