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

  • Home
  • SEARCH
  • 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 1098539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:35:37+00:00 2026-05-17T00:35:37+00:00

Usually one is looking for a way to update the UI while something in

  • 0

Usually one is looking for a way to update the UI while something in the background is still working. My approach there would be either use a new thread or simply use SwingUtilities.invokeLater.

However, right now I’m in the completely opposite situation. I have a button that calls a static method which does some magic (not important here). Pressing the button also starts an animation on the UI thread (currently implemented using a Swing Timer). When the magic method is finished, it first fires an event to the UI and then continues doing something else. The event tells the UI to stop the animation – after some delay (actually the UI is supposed to show a different animation for a short time).

That delay however is exactly the problem, because I want to prevent the magic method from continuing before the animation has finished. The event listener call is currently implemented synchronizely, so the event listener won’t return before it completes. But as the animation is asynchronous, I don’t know how to block the background process for that time. I thought I could use a ReentrantLock to make it synchronized, but when I tried to lock it from the UI thread, that lock was ignored.

What is the correct way to make the background process wait for the UI?

edit

The code below shows extremely simplified what happens. The UI is just one of the actual listeners (the only blocking one though), so I would rather have some blocking effect on the UI side of the event handling.

public class Worker
{
    public static void magicMethod()
    {
        // do something
        // ...

        // fire event
        for ( ExampleEventListener listener : listeners )
            listener.onExampleEvent( new ExampleEvent() );

        // BLOCK until animation in UI completed
        nextStep();
    }

    public static void nextStep ()
    {
        // continue work
    }
}


public class MyWindow extends JFrame implements ExampleEventListener
{
    public void actionPerformed ( ActionEvent event )
    {
        // button clicked
        Worker.magicMethod();
    }

    public void onExampleEvent ( ExampleEvent event )
    {
        startAsynchronousAnimation();
    }

    private void completeAnimation ()
    {
        // animation completed
        animationPanel.setVisible( false );

        // UNLOCK
    }
}
  • 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-17T00:35:37+00:00Added an answer on May 17, 2026 at 12:35 am

    Not sure if its the best way, but you could use a shared boolean value to signal if the background process is ready to continue. Using wait() will cause the thread to release the lock an object in a synchronized block. Something like:

    while(! ready){
        try{
            mySynchonizedObject.wait(); 
        }catch(InterruptedException ex){
            System.exit(1);
        }
    }
    

    Where ‘ready’ is a shared boolean value. When you UI is ready it would then set ready to true and call notifyAll().

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

Sidebar

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.